Normally when you retrieve a JWT for authentication you get the token itself, but also an expired_at timestamp. You can use this timestamp to determine if the user needs to be login again or not. You can save this in your storage as well ;)
Determine whether user is allowed to enter route (Laravel, JWT, VueRouter)
Hi community.
For the first time I'm trying out Laravel and Vue together, and I've watched some series here on Laracasts despite the fact that some of the series is a bit outdated. Gave me a good insight.
Right now I'm struggling around how to determine whether the user is allowed to enter a page.
I have this snippet that runs before the VueRouter is entering a page (right now is userIsAuthenticated hardcoded to false for testing purposes):
router.beforeEach(async (to, from, next) => {
const userIsAuthenticated = false
if (to.matched.some(record => record.meta.requiresAuth)) {
if (!userIsAuthenticated) {
return next({
path: '/login',
params: { nextUrl: to.fullPath }
})
}
return next()
} else {
if (userIsAuthenticated && to.name === 'login') {
return next({
path: '/',
params: { nextUrl: to.fullPath }
})
}
return next()
}
})
On some pages I have several API calls, that requires authentication (Bearer token from JWT) - in that case I can redirect back to the login page, if the status code is either 401 or 403.
But what if I don't have any API calls requiring authentication on a page that requires that you are logged in? In this way I can't grab the JWT from my storage and classify it as valid proof of authentication, because the client side does not know anything about it's validity. How do I handle this scenario?
I've thought of some kind of "verification" endpoint, to check the tokens validity, every time the user navigates to a new page. But I think this as some kind of overhead or unnecessary complexity. I just can't get my head around any other solutions.
I'm not that skilled in Vue/JavaScript yet - so maybe there is some kind of standard way of handling this. But I can't Google me to it.
Thanks in advance
Please or to participate in this conversation.