harsh-harlequin
harsh-harlequin•6mo ago

anyways i can make logout quicker ?

everything working fine but after 401 it takes too much to logout user
logout: async () => {
localStorage.removeItem(TOKEN_KEY)
return {
success: true,
redirectTo: '/'
}
},
check: async () => {
const token = localStorage.getItem(TOKEN_KEY)
if (token) {
axiosInstance.defaults.headers.common['Authorization'] = `Bearer ${token}`
return {
authenticated: true
}
}

return {
authenticated: false,
logout: true,
error: new Error('Token not found'),
redirectTo: '/'
}
},
getPermissions: async () => null,
getIdentity: async () => {
const token = localStorage.getItem(TOKEN_KEY)
const username = localStorage.getItem(USER_NAME)
if (token) {
return {
username
}
}
return null
},
onError: async error => {
const status = error.response?.status
if (status === 401 || status === 403) {
return {
logout: true,
redirectTo: '/login',
error
}
}

if (status === 400) {
return {}
}

return {}
}
logout: async () => {
localStorage.removeItem(TOKEN_KEY)
return {
success: true,
redirectTo: '/'
}
},
check: async () => {
const token = localStorage.getItem(TOKEN_KEY)
if (token) {
axiosInstance.defaults.headers.common['Authorization'] = `Bearer ${token}`
return {
authenticated: true
}
}

return {
authenticated: false,
logout: true,
error: new Error('Token not found'),
redirectTo: '/'
}
},
getPermissions: async () => null,
getIdentity: async () => {
const token = localStorage.getItem(TOKEN_KEY)
const username = localStorage.getItem(USER_NAME)
if (token) {
return {
username
}
}
return null
},
onError: async error => {
const status = error.response?.status
if (status === 401 || status === 403) {
return {
logout: true,
redirectTo: '/login',
error
}
}

if (status === 400) {
return {}
}

return {}
}
3 Replies
kapa.ai
kapa.ai•6mo ago
Hi @Tarang. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The delay in logging out the user after a 401 error might be due to the time it takes for the onError method to execute and return a response. In your onError method, you're checking for a 401 or 403 status and then returning an object with logout: true and redirectTo: '/login'. This triggers the logout process. However, if the status is 400, you're returning an empty object {}. This might be causing some delay if the server is returning a 400 status. You might want to handle this case differently. Also, ensure that your check method is working correctly. This method is used to check the authentication status of the user. If the token is not found in the local storage, it returns an object with authenticated: false and logout: true, which should trigger the logout process. Here's a reference to the logout method in the authProvider from the Refine documentation (https://refine.dev/docs/authentication/auth-provider#logout):
logout: async () => {
localStorage.removeItem("auth");
return {
success: true,
redirectTo: "/login",
};
},
logout: async () => {
localStorage.removeItem("auth");
return {
success: true,
redirectTo: "/login",
};
},
And the useLogout hook:
import { useLogout } from "@refinedev/core";

const { mutate: logout } = useLogout();

logout({
redirectPath: "/custom-url",
},
{
onSuccess: (data) => {
if (!data.success) {
// handle error
}
// handle success
},
},
);
import { useLogout } from "@refinedev/core";

const { mutate: logout } = useLogout();

logout({
redirectPath: "/custom-url",
},
{
onSuccess: (data) => {
if (!data.success) {
// handle error
}
// handle success
},
},
);
If the delay persists, it might be due to other factors not directly related to the logout process, such as network latency or server response time. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai•6mo ago
<@852840146495340564> kapa.ai is still learning and improving, please let us know how it did by reacting below
rival-black
rival-black•6mo ago
@Tarang We need more information for this issue, there can be many reasons why something is slow 🤔 If you provide reproducible example, we would happy to debug