Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DRF token from python-social-auth backend

I am trying to integrate Social Authentication in my DRF backend. I decided to go with python-social-auth. If I serve my social login through Django (and an HTML view), I can see my login in successful. I also figured that I can redirect after successful social authentication as outlined here.

Until now, my frontend (a Nuxt app) was using DRF tokens. Even though:

  1. I can create valid tokens for social accounts.
  2. My frontend can redirect the users to -> complete authorization with OAuth sites e.g. Google, Twitter -> return back to the frontend.

Is it possible for me to somehow manage to redirect a successfully authenticated user to the frontend with the associated and valid DRF token as a query parameter?

like image 721
DaveIdito Avatar asked Dec 19 '25 10:12

DaveIdito


1 Answers

You can, it's not trivial, tho. It's possible because the mechanism to retrieve URLs (success or errors ones) is delegated to strategies setting() method, which in the end invokes get_setting() on final implementations. This method you can override to add your custom logic.

These steps should get you on the road:

  1. Define a custom strategy

    from social_django.strategy import DjangoStrategy
    
    
    class CustomStrategy(DjangoStrategy):
        def get_setting(self, name):
            if name == 'LOGIN_REDIRECT_URL':
                token = get_drf_token()
                return f'/?toke={token}'
             else:
                 return super().get_setting(name)
    
  2. Point your SOCIAL_AUTH_STRATEGY settings to this new strategy (import path syntax)

like image 115
omab Avatar answered Dec 21 '25 00:12

omab