An authenticated user can still access the /login
page. If I click on a link to the /login
page, I am redirected to a different page successfully. However, if I manually type in /login
in the URL, I am still taken to the login page even though I am already logged in. My goal is to redirect the user to the /retailer/account
page before any components are displayed on the page when they are logged in and try to access the /login
page manually.
I have tried using the beforeMount()
function and the beforeCreate()
function following the documentation for the auth: 'guest' middleware, but it does not seem to work as expected, as the loggedIn
always returns false
before the page is fully rendered.
Here is my setup:
nuxt.config.js
:
export default {
mode: 'universal',
target: 'static',
auth: {
cookie: {
prefix: 'auth_'
},
// Options
redirect: {
login: '/login',
logout: '/login',
callback: false,
home: '/retailer/account'
},
strategies: {
local: {
endpoints: {
login: {
url: '/auth/login',
method: 'post',
propertyName: 'access_token',
credentials: true
},
logout: {
url: '/auth/logout',
method: 'post'
},
user: {
url: '/auth/me',
method: 'post',
propertyName: '',
credentials: false
}
},
token: {
required: true,
type: 'Bearer',
global: true
},
autoFetchUser: true
}
},
preserveState: true,
watchLoggedIn: true
},
router: {
middleware: [
'auth'
]
}
}
layouts/default.vue
:
export default {
auth: false
}
pages/retailer/account.vue
:
export default {
middleware: 'auth'
}
pages/login.vue
:
export default {
middleware: 'authenticated',
auth: 'guest'
}
I have tried various middleware examples and implemented some basic redirection code, but the issue persists where app.$auth.loggedIn
always returns false
from the server-side, preventing me from being redirected.
middleware/authenticated.js
export default function ({ app, redirect }) {
if (app.$auth.loggedIn) {
return redirect(app.localePath({ name: 'index' }))
}
}
After Vue hydrates the app, the loggedIn
naturally becomes true
.
If you have any ideas or guidance on what I could be doing wrong, I would greatly appreciate it!