mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2025-07-22 05:29:46 +03:00
Refactored login process
This commit is contained in:
parent
6e940e08aa
commit
d6e97cc037
|
@ -7,6 +7,7 @@ from rest_framework.permissions import AllowAny
|
||||||
from rest_framework.generics import CreateAPIView
|
from rest_framework.generics import CreateAPIView
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
|
from allauth.account.adapter import get_adapter
|
||||||
from allauth.account.views import ConfirmEmailView
|
from allauth.account.views import ConfirmEmailView
|
||||||
from allauth.account.utils import complete_signup
|
from allauth.account.utils import complete_signup
|
||||||
from allauth.account import app_settings as allauth_settings
|
from allauth.account import app_settings as allauth_settings
|
||||||
|
@ -101,3 +102,6 @@ class SocialLoginView(LoginView):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
serializer_class = SocialLoginSerializer
|
serializer_class = SocialLoginSerializer
|
||||||
|
|
||||||
|
def process_login(self):
|
||||||
|
get_adapter(self.request).login(self.request, self.user)
|
||||||
|
|
|
@ -15,7 +15,7 @@ class FacebookLogin(SocialLoginView):
|
||||||
adapter_class = FacebookOAuth2Adapter
|
adapter_class = FacebookOAuth2Adapter
|
||||||
|
|
||||||
|
|
||||||
class TwitterLogin(LoginView):
|
class TwitterLogin(SocialLoginView):
|
||||||
adapter_class = TwitterOAuthAdapter
|
adapter_class = TwitterOAuthAdapter
|
||||||
serializer_class = TwitterLoginSerializer
|
serializer_class = TwitterLoginSerializer
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
from django.contrib.auth import login, logout
|
from django.contrib.auth import (
|
||||||
|
login as django_login,
|
||||||
|
logout as django_logout
|
||||||
|
)
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
@ -11,7 +14,6 @@ from rest_framework.permissions import IsAuthenticated, AllowAny
|
||||||
from rest_framework.generics import RetrieveUpdateAPIView
|
from rest_framework.generics import RetrieveUpdateAPIView
|
||||||
|
|
||||||
from allauth.account import app_settings as allauth_settings
|
from allauth.account import app_settings as allauth_settings
|
||||||
from allauth.account.adapter import get_adapter
|
|
||||||
|
|
||||||
from .app_settings import (
|
from .app_settings import (
|
||||||
TokenSerializer, UserDetailsSerializer, LoginSerializer,
|
TokenSerializer, UserDetailsSerializer, LoginSerializer,
|
||||||
|
@ -38,6 +40,9 @@ class LoginView(GenericAPIView):
|
||||||
serializer_class = LoginSerializer
|
serializer_class = LoginSerializer
|
||||||
token_model = TokenModel
|
token_model = TokenModel
|
||||||
|
|
||||||
|
def process_login(self):
|
||||||
|
django_login(self.request, self.user)
|
||||||
|
|
||||||
def get_response_serializer(self):
|
def get_response_serializer(self):
|
||||||
if getattr(settings, 'REST_USE_JWT', False):
|
if getattr(settings, 'REST_USE_JWT', False):
|
||||||
response_serializer = JWTSerializer
|
response_serializer = JWTSerializer
|
||||||
|
@ -54,7 +59,7 @@ class LoginView(GenericAPIView):
|
||||||
self.token = create_token(self.token_model, self.user, self.serializer)
|
self.token = create_token(self.token_model, self.user, self.serializer)
|
||||||
|
|
||||||
if getattr(settings, 'REST_SESSION_LOGIN', True):
|
if getattr(settings, 'REST_SESSION_LOGIN', True):
|
||||||
get_adapter(self.request).login(self.request, self.user)
|
self.process_login()
|
||||||
|
|
||||||
def get_response(self):
|
def get_response(self):
|
||||||
serializer_class = self.get_response_serializer()
|
serializer_class = self.get_response_serializer()
|
||||||
|
@ -109,7 +114,7 @@ class LogoutView(APIView):
|
||||||
except (AttributeError, ObjectDoesNotExist):
|
except (AttributeError, ObjectDoesNotExist):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
logout(request)
|
django_logout(request)
|
||||||
|
|
||||||
return Response({"success": _("Successfully logged out.")},
|
return Response({"success": _("Successfully logged out.")},
|
||||||
status=status.HTTP_200_OK)
|
status=status.HTTP_200_OK)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user