-
Notifications
You must be signed in to change notification settings - Fork 887
fix: redirect to login upon authentication error #9134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
I have read the CLA Document and I hereby sign the CLA |
What I would do, in this particular case, is to add an Axios interceptor in the |
PS: I don't think this a great way of doing it but the best I can think 🤔 feel free to propose any refactor if you think we should deal with that differently |
it's definitely better than doing it in some random component that may or may not always get rendered :p I think this is the kind of issue that's fine to fix in a "good enough" way and improve later if it becomes an issue |
so after a bit more playing around with this, I'm not sure that I like handling this from outside of React... it makes it feel a lot more fragile, and requires that you check manually for any routes that shouldn't require authentication (like /login and /setup) or you can get stuck with buggy redirects. I'd really love to figure out a non-invasive way to have the |
@aslilac this is the ideal scenario. Maybe, you might be able to add the interceptor in the RequireAuth component using a // RequireAuth.tsx
const navigate = useNavigate()
useEffect(() => {
// This is just a mock code, Idk what is the exact Axios API to do that
const interceptor = axios.interceptor((_req, res, next) => {
if(res.code === 401) {
redirectToLoginWithCurrentUrl()
return
}
next()
})
return () => {
interceptor.remove()
}
}) |
clever! best of both worlds! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐑 it
Fixes #9090
This is like, way very much the wrong place to do this. I'd love some feedback on where a more appropriate place might be from someone more familiar with the codebase.Do we use 401 codes for any other circumstance? (ie. properly logged in, but not enough permission) How can we disambiguate?I tried usingNavigate
,useNavigate
, etc. from react-router-dom and they all seem to just...not work, for some reason. 🙃 I spent like 15 minutes trying to debug it and got nowhere.Checks
detail
to make sure it's an expired token, because we use 401 for several thingsAdds an interceptor to axios when the
RequireAuth
component is mounted to check for any responses failing because of expired auth.Updates the auth state to signed out when detected, which will automatically kick users out to the login screen (and will display the error message that resulted in this behavior)