I've been used Nuxt.js(v2.15.7) as SPA web apps. But Redirecting doesn't work in my apps.
My apps is simple. User login the apps, and then move to home contents. login display made in pages/index.vue, and home contents made in pages/home.vue.
and I also have used "vuex" as state management. So, I made redirect process like below.
export default ({ store, redirect, route }: any) => {
if (!store.state.user && (route.name !== 'index' || route.name !== 'auth-setPassword')) {
redirect('/')
}
if (store.state.user && (route.name === 'index' || route.name === 'auth-setPassword')) {
redirect('/home')
}
}
If user finish login process, user info preserve state.user. Of user doen't finish login process, state.user always is null. this file placed in the plugins. and then add plugins file path in nuxt.config.ts like below.
export default{
・・・
plugins: [
{ src: '~/plugins/auth', ssr: false },
],
・・・
}
but it doesn't work. I always try like "localhost:3000/home" not logged in and move to the "home" contents.
and I try to use "window.onNuxtReady" in my redirect process like below.
export default ({ store, redirect, route }: any) => {
if (!store.state.user && (route.name !== '/' || route.name !== 'auth-setPassword')) {
(<any>window).onNuxtReady(() => {
redirect('/')
})
}
if (store.state.user && (route.name === '/' || route.name === 'auth-setPassword')) {
(<any>window).onNuxtReady(() => {
redirect('/home')
})
}
}
Redirect process was success but always show error message below.
NavigationDuplicated: Avoided redundant navigation to current location: "/".
Does anyone help me, how to redirect correctly in nuxt.js web apps?