Customize django-allauth social login and call back URL (redirect them through a front-end service )

Viewed 935

When I sign up/sign in with my social account using django-allauth like

http://localhost:8000/accounts/google/login/

Now after doing authentication, Google redirect us back to the below callback URL as

http://localhost:8000/accounts/microsoft/login/callback/

I want to change the above URLs to be redirected to the frontend service and then redirect it from there to our backend server. Is there a way around to achieve this? Because due to security reasons I only our frontend server to interact with our back, not any other third part.

1 Answers

I'm not sure what type of authentication you are using in your application (between frontend and backend) but using the allauth default callback will help you to create or login users automatically without extra effort. If you want to handle the callback in the frontend, you just need to change the callback URL in your provider (For example in google, it is where you obtain your client ID) to a URL that is located in your frontend application and then, sends the callback parameters to the backend manually.

Here is what you should do (it is assumed that your frontend is hosted on example.com and your backend is in api.example.com):

redirect URL: example.com/google/callback?{some_parameters_sent_by_google}

In your frontend application, you should get the parameters from the URL and then, send the parameters to the backend (in a POST request for example.)

Example Request:

URL: api.example.com/login/social/ Body:

{
'social': 'google',
'parameters': {some_parameters_sent_by_google(as json)}
}

And then, in your backend, you can handle the login or signup manually.

Also, if you want to stick to allauth, you can use the custom redirect but it won't satisfy your need to limit the backend-frontend interaction.

Related