stormy-gold
stormy-gold2y ago

Catch 401 error

Hi, how can i catch a 401 error and redirect to a specific page, by default it goes to the login page. I want to show some kind of message/notification when i get 401 error , different from the base notification from the notification provider.
9 Replies
magic-amber
magic-amber2y ago
Hi 👋 . You can use this for custom redirectPath. also you can add name and message for noification
const authProvider: AuthProvider = {
...
checkError: (error) => {
if (error.status === 401) {
return Promise.reject({
redirectPath: "/custom-page",
name: "Custom Error",
message: "Something went wrong!",
});
}

return Promise.resolve();
},
...
}
const authProvider: AuthProvider = {
...
checkError: (error) => {
if (error.status === 401) {
return Promise.reject({
redirectPath: "/custom-page",
name: "Custom Error",
message: "Something went wrong!",
});
}

return Promise.resolve();
},
...
}
stormy-gold
stormy-goldOP2y ago
For some reason this doesn't work. These are the versions i use, if it matters "@pankod/refine-antd": "^3.62.0", "@pankod/refine-core": "^3.88.0", "@pankod/refine-react-router-v6": "^3.36.2", "@pankod/refine-simple-rest": "^3.35.0",
magic-amber
magic-amber2y ago
Sorry for your trouble. i will debug and return you as soon as possible
stormy-gold
stormy-goldOP2y ago
Ok, thank you
magic-amber
magic-amber2y ago
can you try this ? simple-rest using axios. and axios returns status in response object
if (error.response.status === 401)
if (error.response.status === 401)
stormy-gold
stormy-goldOP2y ago
Yes, this work,i can catch the 401 error, but the redirect doesn't seem to work
checkError: (error) => {
if (error?.response?.status === 401) {
return Promise.reject("/login?error=401");
}
return Promise.resolve();
},
checkError: (error) => {
if (error?.response?.status === 401) {
return Promise.reject("/login?error=401");
}
return Promise.resolve();
},
I'm looking at the documentation here https://refine.dev/docs/api-reference/core/hooks/auth/useCheckError/ but when i reject the promise , every time it goes to the /login page
magic-amber
magic-amber2y ago
Sorry, my first code example was wrong. i reproduced this code. it goes here as expected. https://hgdwf9.csb.app/login?error=401 you can check project here : https://codesandbox.io/s/hardcore-golick-wrnrom?file=/src/App.tsx can you update refine core to latest version ? probably this bug fixed in here https://github.com/refinedev/refine/commit/e3642eafa215e354f605c57ec380bc04e1c27812
stormy-gold
stormy-goldOP2y ago
I updated the version and it works now, thank you !
magic-amber
magic-amber2y ago
I'm glad to hear that 🙏