adverse-sapphire
adverse-sapphire17mo 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
like-gold
like-gold17mo 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();
},
...
}
adverse-sapphire
adverse-sapphire17mo 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",
like-gold
like-gold17mo ago
Sorry for your trouble. i will debug and return you as soon as possible
adverse-sapphire
adverse-sapphire17mo ago
Ok, thank you
like-gold
like-gold17mo 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)
adverse-sapphire
adverse-sapphire17mo 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
like-gold
like-gold17mo 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
adverse-sapphire
adverse-sapphire17mo ago
I updated the version and it works now, thank you !
like-gold
like-gold17mo ago
I'm glad to hear that 🙏