Lets take a system where a jwt token is saved to local storage upon login and now we check for the token existence to allow a user access to a protected route.
We're using react-router-redux and bundling everything with webpack and our code is in the client/src folder as apposed to the server folder.
So the protected route with our auth check would look something like the following (let's say we send our routes only on demand) :
import { push } from 'react-router-redux'
function requireAuth(store) {
const check = localStorage.jwt
check ? store.dispatch(push('/dashboard')) : store.dispatch(push('/'))
}
export default (store) => ({
path: 'dashboard',
onEnter: requireAuth(store),
getComponent (nextState, cb) {
require.ensure([], (require) => {
const Dashboard = require('./containers/DashboardContainer').default
cb(null, Dashboard)
}, 'dashboard')
}
})
And for completion our index routes file:
import Login from './login'
import Dashboard from './Dashboard'
import Logout from './Logout'
export const createRoutes = (store) => ({
path: '/',
indexRoute: Login,
childRoutes: [
Dashboard(store), //Our protected route
Logout(store)
]
})
export default createRoutes
Putting a side man in the middle attacks and the inherint problems with the nature of javascript crypto methos as discussed here: SPA best practices for authentication and session management
I would like to focus on the above illustrated method of a function placed on the onEnter to allow access:
If I understand correctly the above example is bundled and sent to the client, Now I might be wrong and the route definitions aren't actually being sent in the bundle. So I guess the question is: does a user has access or can see our above route definitions by default - if so how do we prevent it?
Also in regards to our auth check in the example, which I've seen used and simply redirects if a token is not present in the local storage, is that really sufficient prevention?
Comparing the above example to using a high order function to wrap our protected component with, so we can preform auth checks in the life cycle methods WillMount and WillRecieveProps as you can see here for example.
I would think that both approaches would suffer from being exposed to the client. Am I right?
What other caveats am I missing regarding each approach?