Nuxt 3 de tokenin süresi dolduğunda kullanıcıyı logout yapmak istiyorum fakat bir türlü yapamadım.
Axios ile yaptım:
axios.interceptors.response.use(
(response) => response,
(error) => {
console.log(error, "ERRRORR");
if (error.response && error.response.status === 401) {
toast.error("Your token has expired, forwarding login page", {
position: "top-right",
timeout: 3000,
closeButton: "button",
icon: true,
rtl: false,
});
setTimeout(() => {
authStore.logout();
router.push("/login");
}, 3000);
}
return Promise.reject(error);
}
);
olmadı.
export default defineNuxtPlugin(nuxtApp => {
// Global error handler
nuxtApp.provide('handleError', (error) => {
if (error.response?.status === 401) {
// Show Toast or Notification
nuxtApp.$toast.error('Token expired, redirecting to login...');
setTimeout(() => {
// Handle logout
nuxtApp.$auth.logout();
// Redirect to login
nuxtApp.$router.push('/login');
}, 3000);
}
});
});
böyle yaptım olmadı. Yardımcı olursanız sevinirim.