N
Nuxt9mo ago
schmidi

navigateTo inside plugin not working

Hi! I need some help with using navigateTo inside a plugin. In my fetch-method i have onResponseError which looks for a 401 status. Works fine, however my navigateTo won't redirect me to the login page. I tried with two different methods, but both won't work.
async onResponseError({ response }) {
if (response.status === 401) {
console.log("401") // gets printed

await callWithNuxt(nuxtApp, navigateTo, [
"/user/login",
{ replace: true, redirectCode: 401 },
])
}
}
async onResponseError({ response }) {
if (response.status === 401) {
console.log("401") // gets printed

await callWithNuxt(nuxtApp, navigateTo, [
"/user/login",
{ replace: true, redirectCode: 401 },
])
}
}
onResponseError({ response }) {
if (response.status === 401) {
console.log("401") // gets printed

return navigateTo("/user/login")
}
}
onResponseError({ response }) {
if (response.status === 401) {
console.log("401") // gets printed

return navigateTo("/user/login")
}
}
Maybe someone can help me figuring out why it won't reconnect me. Thanks!
1 Reply
Joey
Joey8mo ago
Maybe try the first approach again but with declaring nuxtApp.
async onResponseError({ response }) {
if (response.status === 401) {
const nuxtApp = useNuxtApp()
console.log("401") // gets printed

await callWithNuxt(nuxtApp, navigateTo, [
"/user/login",
{ replace: true, redirectCode: 401 },
])
}
}
async onResponseError({ response }) {
if (response.status === 401) {
const nuxtApp = useNuxtApp()
console.log("401") // gets printed

await callWithNuxt(nuxtApp, navigateTo, [
"/user/login",
{ replace: true, redirectCode: 401 },
])
}
}
Want results from more Discord servers?
Add your server