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
Add dependency to redirect unauthenticated users to login and add webpages to display error responses. #26
Conversation
0a92a5b
to
b7720a9
Compare
b7720a9
to
78b5318
Compare
To redirect users on the login page we can do it in two ways.
@Half-Shot @Cadair |
if (axios.isAxiosError(er)) { | ||
if (er.response) { | ||
if (er.response.status == 401) { | ||
history.replace("/login"); | ||
} | ||
} | ||
} |
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.
Saves you a bit of nesting. You can use foo?.bar
to say if foo is undefined, don't try to access bar
. Very handy for these kinds of logic.
if (axios.isAxiosError(er)) { | |
if (er.response) { | |
if (er.response.status == 401) { | |
history.replace("/login"); | |
} | |
} | |
} | |
if (axios.isAxiosError(er) && er.response?.status == 401) { | |
history.replace("/login"); | |
} |
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.
Done, thanks.
return response; | ||
}, | ||
function (er) { | ||
if (axios.isAxiosError(er)) { |
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.
Wonder if this is a good thing to explicitly log, so we know why we've been navigated away?
frontend/src/Pages/ExternalUrl.js
Outdated
</div> | ||
</div> | ||
<div> | ||
<img src="https://i.ibb.co/ck1SGFJ/Group.png" /> |
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.
I spot another cheeky remote asset :p
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.
Done :D
frontend/src/Pages/LoginSuccess.js
Outdated
if(loginToken == null){ | ||
throw new Error("Invalid login token."); | ||
} | ||
fetchData(); |
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.
Always await these.
fetchData(); | |
await fetchData(); |
f1cad3b
to
992b84b
Compare
…nticate_user` dependency. Unauthenticated users are redirected to login page (full reload of SPA occurs). Sessions return a null value when session_id is not present in redis. Update client-side API calls and backend tests to handle url changes.
These interceptors get triggered on every response and if the status code is 401, the user is redirected to login. All axios imports are replaced with a custom axios instance.
78b5318
to
229f001
Compare
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.
LGTM
Parent: #25
/login
endpoint.I added a dependency in the backend which acts as middleware and checks if the request has a valid session ID. If it doesn't, then it returns a "401 unauthorized request" as a response.
To handle this on the client-side I created an Axios interceptor which checks for a 401 error in all the incoming responses. If present, it'll redirect the user to the login page.
Demo pages for 404 and External URL response messages:
error-404.png
External Url invite message.