Improve login and authentication methods to adapt to the SSR mode.
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||
// skip middleware on server
|
||||
if (process.server) return
|
||||
|
||||
const user = useUser()
|
||||
const signInPath = '/account/signin'
|
||||
if (!user.value && to.path !== signInPath) {
|
||||
const error = await fetchUser()
|
||||
console.log(error)
|
||||
if (error) {
|
||||
const { error, data} = await fetchUser()
|
||||
if (error.value) {
|
||||
return navigateTo({
|
||||
path: signInPath,
|
||||
query: {
|
||||
callback: encodeURIComponent(to.fullPath)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
setUser(data.value)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user