I am currently working on implementing an OAuth2 flow for a Single Page Webapp, but I am facing challenges in dealing with Frontend/JavaScript redirects.
Regarding the backend setup, I have it all sorted out: utilizing a library that takes care of everything and provides 2 routes:
/auth/authorize
returns a JSON response as follows:{ "authorization_url": "https://github.com/login/oauth/authorize?response_type=code&client_id=d263f54&redirect_uri=http%3A%2F%2Fxxx&state=eyJ0eXAiOiJkdjsfkj&scope=read" }
/auth/callback
is called back as theredirect_uri
by the OAuth provider (such as Github), returning either a JSON response like:{ "access_token":"jhfgkjhdfkjghkdhgkdhgfd","token_type":"bearer"}
(if Bearer authentication is used), or simply
null
(in case of Cookie authentication. In this scenario, the cookie is appropriately set on the browser)
How should I go about handling this on the frontend side?
When a user clicks the Login button, I can easily redirect them to the OAuth provider login page using:
window.open(axios.get('/auth/authorize').data.authorization_url);
without any issues.
However, if I register the callback in my OAuth provider as /auth/callback
, the user will end up on a plain text page after redirection, which is not ideal.
I believe I need to set up a Frontend route as the OAuth provider callback, where this route listens to the response from the OAuth provider and forwards the exact same response to the backend's auth/callback
.
So, how can I achieve this using Axios/Javascript?
Is there a recommended/best practice approach for this situation?
Thank you!
(For additional information, the backend library I am using is FastAPI-Users and the frontend is built using Vue.js)