python - Plug in django-allauth as endpoint in django-rest-framework -


i'm using django-allauth on website social logins. have rest api powered django-rest-framework serves backend of mobile app. there way can directly plug in allauth's authentication backend rest api can validate (and register) users use facebook login in mobile app?

to clarify: facebook login part handled native sdks. need endpoint works post /user (that is, creates new user), takes facebook oauth token input instead of email/password etc.

you can use libray social authentication django-rest-framework-social-oauth2. try django-allauth related code

urls.py

urlpatterns = [     url(         r'^rest/facebook-login/$',         csrf_exempt(restfacebooklogin.as_view()),         name='rest-facebook-login'     ), ] 

serializers.py

class everybodycanauthentication(sessionauthentication):     def authenticate(self, request):         return none 

views.py

class restfacebooklogin(apiview):     """     login or register user based on authentication token coming     facebook.     returns user data including session id.     """      # public api!!!     permission_classes = (allowany,)     authentication_classes = (everybodycanauthentication,)      def dispatch(self, *args, **kwargs):         return super(restfacebooklogin, self).dispatch(*args, **kwargs)      def get(self, request, *args, **kwargs):         try:             original_request = request._request             auth_token = request.get.get('auth_token', '')              # find token matching passed auth token             app = socialapp.objects.get(provider='facebook')             fb_auth_token = socialtoken(app=app, token=auth_token)              # check token against facebook             login = fb_complete_login(original_request, app, fb_auth_token)             login.token = fb_auth_token             login.state = sociallogin.state_from_request(original_request)              # add or update user users table             complete_social_login(original_request, login)             # create or fetch session id user             token, _ = token.objects.get_or_create(user=original_request.user)             # if here we've succeeded             data = {                 'username': original_request.user.username,                 'objectid': original_request.user.pk,                 'firstname': original_request.user.first_name,                 'lastname': original_request.user.last_name,                 'sessiontoken': token.key,                 'email': original_request.user.email,             }             return response(                 status=200,                 data=data             )          except:             return response(status=401, data={                 'detail': 'bad access token',             }) 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -