mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2025-07-23 05:59:45 +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.response import Response
|
||||
from rest_framework.permissions import AllowAny
|
||||
from rest_framework.authtoken.models import Token
|
||||
from rest_framework import status
|
||||
|
||||
from allauth.account.views import SignupView, ConfirmEmailView
|
||||
from allauth.account.utils import complete_signup
|
||||
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.views import Login
|
||||
from rest_auth.views import (
|
||||
Login,
|
||||
EverybodyCanAuthentication,
|
||||
)
|
||||
|
||||
|
||||
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,)
|
||||
user_serializer_class = UserDetailsSerializer
|
||||
authentication_classes = (EverybodyCanAuthentication,)
|
||||
token_model = Token
|
||||
token_serializer = TokenSerializer
|
||||
user_serializer = UserDetailsSerializer
|
||||
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
|
@ -27,6 +48,8 @@ class Register(APIView, SignupView):
|
|||
|
||||
def form_valid(self, form):
|
||||
self.user = form.save(self.request)
|
||||
self.token, created = self.token_model.objects.get_or_create(
|
||||
user=self.user)
|
||||
if isinstance(self.request, HttpRequest):
|
||||
request = self.request
|
||||
else:
|
||||
|
@ -47,8 +70,10 @@ class Register(APIView, SignupView):
|
|||
return self.get_response_with_errors()
|
||||
|
||||
def get_response(self):
|
||||
serializer = self.user_serializer_class(instance=self.user)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
response = self.token_serializer(self.token).data
|
||||
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):
|
||||
return Response(self.form.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
|
|
@ -27,16 +27,18 @@ class EverybodyCanAuthentication(SessionAuthentication):
|
|||
|
||||
|
||||
class Login(GenericAPIView):
|
||||
|
||||
"""
|
||||
Check the credentials and return the REST Token
|
||||
and the user object
|
||||
if the credentials are valid and authenticated.
|
||||
Calls Django Auth login method to register User ID
|
||||
in Django session framework
|
||||
|
||||
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,)
|
||||
authentication_classes = (EverybodyCanAuthentication,)
|
||||
serializer_class = LoginSerializer
|
||||
|
|
Loading…
Reference in New Issue
Block a user