Closed luminous8 closed 3 months ago
U can try it.
const { loggedIn, fetch } = useUserSession()
// ------------------------------------------
const [err] = await to($fetch('/api/login', {
method: 'POST',
body: {
username: model.username,
password,
},
}))
logining.value = false
if (err) {
message.type = 'error'
message.content = err instanceof FetchError ? err.data.message : err.message
}
else {
message.type = 'success'
message.content = 'It's ok'
// ******************
fetch().then(() => {
router.push('/dashboard')
})
// ******************
}
}
I looked at the source code. Although I don't know if this approach is appropriate, it does work properly in the middleware.
U can try it.
const { loggedIn, fetch } = useUserSession() // ------------------------------------------ const [err] = await to($fetch('/api/login', { method: 'POST', body: { username: model.username, password, }, })) logining.value = false if (err) { message.type = 'error' message.content = err instanceof FetchError ? err.data.message : err.message } else { message.type = 'success' message.content = 'It's ok' // ****************** fetch().then(() => { router.push('/dashboard') }) // ****************** } }
I looked at the source code. Although I don't know if this approach is appropriate, it does work properly in the middleware.
Thanks! But does that mean I've got to make a call on each route to validate that the user session is valid? 🤔 I thought it was stored in the cookies, and I could take advantage of this
After logging in successfully, you only need to call it once. When logging out, call clear(). In other places, you can check response.status for network request responses.
The issue was on my end; I had a problem with middleware that caused the issue.
Thanks @CynCw for your help 🙏
I exactly have the same issue @luminous8 do you mind sharing your solution?
I exactly have the same issue @luminous8 do you mind sharing your solution?
tbh i don't remember exactly what was the issue - i remember removing all parts from my middleware until it worked, but it wasn't linked to nuxt-auth-utils
@luminous8 thanks for the answer 🙏
My middleware is really basic but impossible to make it working on the landing page "/". Other pages work great:
export default defineNuxtRouteMiddleware((to, from) => {
const { loggedIn } = useUserSession()
if (loggedIn.value) {
return navigateTo(`/cards`)
}
})
By using fetch, I. am able to be redirected, but it take few secondes so my users see the landing and then are redirected after 1 or 2 secondes... It's not ideal but a bit better ^^
export default defineNuxtRouteMiddleware(async (to, from) => {
const { loggedIn, fetch } = useUserSession()
await fetch()
if (loggedIn.value) {
return navigateTo(`/cards`)
}
})
Indeed, the issue must be elsewhere Try removing most things and adding them back gradually to see when the issue appears
Hi,
I have a client
middleware auth.global.js
that looks like thisthat works well but when landing "/", loggedIn is false, user & session are not defined
I've tried to this in index.page without any luck
but when using
loggedIn
oruser
inside it worksi'm pretty sure it's a dumb mistake but i cannot point the issue The issue happens when app is built, works well on dev The index is not pre-rendered nor static / i haven't any routeRules set and ssr is true
What am i missing? Thanks!