mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2025-07-23 22:19:46 +03:00
register update
This commit is contained in:
parent
75f67aad05
commit
2fff7a6254
|
@ -2,21 +2,42 @@ from django.http import HttpRequest
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import AllowAny
|
from rest_framework.permissions import AllowAny
|
||||||
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
from allauth.account.views import SignupView, ConfirmEmailView
|
from allauth.account.views import SignupView, ConfirmEmailView
|
||||||
from allauth.account.utils import complete_signup
|
from allauth.account.utils import complete_signup
|
||||||
from allauth.account import app_settings
|
from allauth.account import app_settings
|
||||||
|
|
||||||
from rest_auth.app_settings import UserDetailsSerializer
|
from rest_auth.app_settings import (
|
||||||
|
UserDetailsSerializer,
|
||||||
|
TokenSerializer,
|
||||||
|
)
|
||||||
from rest_auth.registration.serializers import SocialLoginSerializer
|
from rest_auth.registration.serializers import SocialLoginSerializer
|
||||||
from rest_auth.views import Login
|
from rest_auth.views import (
|
||||||
|
Login,
|
||||||
|
EverybodyCanAuthentication,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Register(APIView, SignupView):
|
class Register(APIView, SignupView):
|
||||||
|
"""
|
||||||
|
Accepts the credentials and creates a new user
|
||||||
|
if user does not exist already
|
||||||
|
Return the REST Token and the user object
|
||||||
|
if the credentials are valid and authenticated.
|
||||||
|
Calls allauth complete_signup method
|
||||||
|
|
||||||
|
Accept the following POST parameters: username, password
|
||||||
|
Return the REST Framework Token Object's key
|
||||||
|
and user object.
|
||||||
|
"""
|
||||||
|
|
||||||
permission_classes = (AllowAny,)
|
permission_classes = (AllowAny,)
|
||||||
user_serializer_class = UserDetailsSerializer
|
authentication_classes = (EverybodyCanAuthentication,)
|
||||||
|
token_model = Token
|
||||||
|
token_serializer = TokenSerializer
|
||||||
|
user_serializer = UserDetailsSerializer
|
||||||
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
|
@ -27,6 +48,8 @@ class Register(APIView, SignupView):
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
self.user = form.save(self.request)
|
self.user = form.save(self.request)
|
||||||
|
self.token, created = self.token_model.objects.get_or_create(
|
||||||
|
user=self.user)
|
||||||
if isinstance(self.request, HttpRequest):
|
if isinstance(self.request, HttpRequest):
|
||||||
request = self.request
|
request = self.request
|
||||||
else:
|
else:
|
||||||
|
@ -47,8 +70,10 @@ class Register(APIView, SignupView):
|
||||||
return self.get_response_with_errors()
|
return self.get_response_with_errors()
|
||||||
|
|
||||||
def get_response(self):
|
def get_response(self):
|
||||||
serializer = self.user_serializer_class(instance=self.user)
|
response = self.token_serializer(self.token).data
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
user = self.user_serializer(instance=self.user).data
|
||||||
|
response['user'] = user
|
||||||
|
return Response(response, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
def get_response_with_errors(self):
|
def get_response_with_errors(self):
|
||||||
return Response(self.form.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(self.form.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
|
@ -27,16 +27,18 @@ class EverybodyCanAuthentication(SessionAuthentication):
|
||||||
|
|
||||||
|
|
||||||
class Login(GenericAPIView):
|
class Login(GenericAPIView):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Check the credentials and return the REST Token
|
Check the credentials and return the REST Token
|
||||||
|
and the user object
|
||||||
if the credentials are valid and authenticated.
|
if the credentials are valid and authenticated.
|
||||||
Calls Django Auth login method to register User ID
|
Calls Django Auth login method to register User ID
|
||||||
in Django session framework
|
in Django session framework
|
||||||
|
|
||||||
Accept the following POST parameters: username, password
|
Accept the following POST parameters: username, password
|
||||||
Return the REST Framework Token Object's key.
|
Return the REST Framework Token Object's key
|
||||||
|
and user object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
permission_classes = (AllowAny,)
|
permission_classes = (AllowAny,)
|
||||||
authentication_classes = (EverybodyCanAuthentication,)
|
authentication_classes = (EverybodyCanAuthentication,)
|
||||||
serializer_class = LoginSerializer
|
serializer_class = LoginSerializer
|
||||||
|
|
Loading…
Reference in New Issue
Block a user