Answer the question
In order to leave comments, you need to log in
How to check jwt in Nuxt, in "spa" mode?
How to check jwt in Nuxt, in "spa" mode?
Colleagues, the problem is as follows:
The application works in "SPA" mode.
Accordingly, server functions, such as "nuxtServerInit" and others, will work naturally.
When a page is loaded for the first time or an already loaded page is updated, the Middleware does not fire, because the Middleware fires once on the server and subsequent transitions ...
But since we work in SPA mode, we are deprived of the possibility of the first Middleware firing when loading.
But I need to protect the routing on first boot.
This is how I do it:
middleware/check-auth.js
export default async function ({store, req, redirect}) {
if (process.browser){
let auth = isAuth(store.getters['access_token'])
if(!auth){
redirect('/login')
}
}
};
export default (ctx) => {
let auth = isAuth(Cookie.get("access_token"))
if(!auth){
ctx.redirect('/login')
} else {
ctx.redirect('/')
}
}
/*
** Plugins to load before mounting the App
*/
plugins: [
{ src: '~/plugins/localStorage.js', ssr: false },
{ src: '~/plugins/nuxt-client-init.js', ssr: false }
],
Answer the question
In order to leave comments, you need to log in
Colleagues, everything was resolved as follows:
Occurs only once when the page is refreshed or when it is first loaded on the client side (in the browser)
export default (ctx) => {
let auth = isAuth(Cookie.get("access_token"))
if(!auth){
ctx.redirect('/login')
} else {
ctx.redirect('/')
}
}
export default async function ({store, req, redirect}) {
if (process.browser){
console.log('check auth')
let auth = isAuth(Cookie.get("access_token"))
if(!auth){
redirect('/login')
}
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question