django-rest-social-auth | OAuth signin with django rest framework | Authentication library
kandi X-RAY | django-rest-social-auth Summary
kandi X-RAY | django-rest-social-auth Summary
[Downloads] OAuth signin with django rest framework.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Handle POST requests
- Get redirect URI
- Returns the object object
- Do login
- Get access token
- Get a JWT token instance
- Get token payload
- Save a social avatar
- Check if an email is provided
- Get refresh token
- Get the token
django-rest-social-auth Key Features
django-rest-social-auth Examples and Code Snippets
Community Discussions
Trending Discussions on django-rest-social-auth
QUESTION
I'm using python-social-auth to allow my users to register via Apple and Google. I'm trying to figure out how via REST I can associate social accounts to an existing user with a different email than the social account (project req is that users are manually created in the backend and then can associate a social login for ease-of-access). This is a React Native app so the usual automatic way of active sessions won't work. I'm using https://github.com/st4lk/django-rest-social-auth to create easy endpoints.
Thanks in advance y'all.
...ANSWER
Answered 2020-Aug-12 at 16:55I got it working. I ended up doing this by creating an associated_email
field on profile model for each social auth service, and writing a customized version of associate by email
step in the pipeline that verifies against that field.
Thanks y'all.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install django-rest-social-auth
Install this package to your python distribution: ```bash pip install rest-social-auth ```
Do the settings Install apps ```python INSTALLED_APPS = ( ... 'rest_framework', 'rest_framework.authtoken', # only if you use token authentication 'social_django', # django social auth 'rest_social_auth', # this package 'knox', # Only if you use django-rest-knox ) ``` social auth settings, look [documentation](http://python-social-auth.readthedocs.io/en/latest/configuration/django.html) for more details ```python SOCIAL_AUTH_FACEBOOK_KEY = 'your app client id' SOCIAL_AUTH_FACEBOOK_SECRET = 'your app client secret' SOCIAL_AUTH_FACEBOOK_SCOPE = ['email', ] # optional SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {'locale': 'ru_RU'} # optional AUTHENTICATION_BACKENDS = ( 'social_core.backends.facebook.FacebookOAuth2', # and maybe some others ... 'django.contrib.auth.backends.ModelBackend', ) ``` Also look [optional settings](#settings) avaliable.
Make sure everything is up do date ```bash python manage.py migrate ```
Include rest social urls (choose at least one) 4.1 [session authentication](http://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication) ```python path('api/login/', include('rest_social_auth.urls_session')), ``` 4.2 [token authentication](http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication) ```python path('api/login/', include('rest_social_auth.urls_token')), ``` 4.3 [jwt authentication](https://github.com/davesque/django-rest-framework-simplejwt) ```python path('api/login/', include('rest_social_auth.urls_jwt_pair')), ``` or / and ```python path('api/login/', include('rest_social_auth.urls_jwt_sliding')), ``` 4.4 [knox authentication](https://github.com/James1345/django-rest-knox/) ```python path('api/login/', include('rest_social_auth.urls_knox')), ```
You are ready to login users Following examples are for OAuth 2.0. 5.1 session authentication POST /api/login/social/session/ input: { "provider": "facebook", "code": "AQBPBBTjbdnehj51" } output: { "username": "Alex", "email": "user@email.com", // other user data } + session id in cookies 5.2 token authentication POST /api/login/social/token/ input: { "provider": "facebook", "code": "AQBPBBTjbdnehj51" } output: { "token": "68ded41d89f6a28da050f882998b2ea1decebbe0" } POST /api/login/social/token_user/ input: { "provider": "facebook", "code": "AQBPBBTjbdnehj51" } output: { "username": "Alex", "email": "user@email.com", // other user data "token": "68ded41d89f6a28da050f882998b2ea1decebbe0" } 5.3 jwt authentication (using [django-rest-framework-simplejwt](https://github.com/davesque/django-rest-framework-simplejwt)) POST /api/login/social/jwt-pair/ POST /api/login/social/jwt-pair-user/ Similar to token authentication, but token is JSON Web Token. See [JWT.io](http://jwt.io/) for details. To use it, django-rest-framework-simplejwt must be installed. For `jwt-pair`, the response will include additional "refresh" token: ```json { "token": "...", "refresh": "..." } ``` ##### Or sliding JWT token: POST /api/login/social/jwt-sliding/ POST /api/login/social/jwt-sliding-user/ Check [docs of simplejwt](https://github.com/davesque/django-rest-framework-simplejwt#token-types) for pair/sliding token difference. Note. `django-rest-framework-simplejwt` doesn't work on python3.6 5.4 knox authentication POST /api/login/social/knox/ POST /api/login/social/knox_user/ Similar to token authentication, but token is Django Rest Knox Token. To use it, [django-rest-knox](https://github.com/James1345/django-rest-knox/) must be installed. User model is taken from [`settings.AUTH_USER_MODEL`](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model). At input there is also non-required field `redirect_uri`. If given, server will use this redirect uri in requests, instead of uri got from settings. This redirect_uri must be equal in front-end request and in back-end request. Back-end will not do any redirect in fact. It is also possible to specify provider in URL, not in request body. Just append it to the url: POST /api/login/social/session/facebook/ Don't need to specify it in body now: { "code": "AQBPBBTjbdnehj51" } Provider defined in URL has higher priority than provider in body. If both are specified - provider will be taken from URL.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page