|
1 |
| -import os |
2 | 1 | import threading
|
3 |
| - |
4 |
| -from django.http import HttpResponseRedirect |
5 |
| -from pytz import timezone |
6 |
| - |
7 |
| -from Apps.Auth.serializers import RegisterSerializer, ChangePasswordSerializer, RegisterWith42Serializer |
| 2 | +from Apps.Auth.serializers import RegisterSerializer, ChangePasswordSerializer |
8 | 3 | from rest_framework.exceptions import AuthenticationFailed
|
9 | 4 | from rest_framework.decorators import api_view, permission_classes
|
10 | 5 | from rest_framework.permissions import *
|
| 6 | +from ..auth_tools import Authenticator, TokenGenerator |
11 | 7 | from ..utils import *
|
12 |
| -from rest_framework_simplejwt.tokens import RefreshToken |
13 |
| -from .permissions import IsEmailVerified |
14 |
| -from ...Profile.api.Serializers import UserSerializer |
15 |
| -from ...Profile.api.api_42 import api_42 |
| 8 | +from ...Profile.api.api_42 import connect_api_42 |
16 | 9 |
|
17 | 10 |
|
18 | 11 | @api_view(['POST'])
|
@@ -40,39 +33,18 @@ def send_email_for_verification(request):
|
40 | 33 | @api_view(['POST'])
|
41 | 34 | @permission_classes([AllowAny])
|
42 | 35 | def email_verification_and_login(request):
|
43 |
| - try: |
44 |
| - username = request.data['username'] |
45 |
| - verification_code = request.data['verification_code'] |
46 |
| - db_verification_code = VerificationCode.objects.get(code=verification_code, username=username) |
47 |
| - user = User.objects.get(username=username) |
48 |
| - except VerificationCode.DoesNotExist: |
49 |
| - return Response(data={'message': 'Invalid verification code!'}, status=400) |
50 |
| - except User.DoesNotExist: |
51 |
| - return Response(data={'message': 'User not found!'}, status=400) |
52 |
| - if verification_code != db_verification_code.code: |
53 |
| - return Response(data={'message': 'Incorrect verification code!'}, status=400) |
54 |
| - |
55 |
| - expiration_time = timedelta(minutes=15) |
56 |
| - if (datetime.now().astimezone(timezone('UTC')) - db_verification_code.expired_date) > expiration_time: |
57 |
| - db_verification_code.delete() |
58 |
| - return Response(data={'message': 'Verification code expired!'}, status=400) |
59 |
| - db_verification_code.delete() |
60 |
| - |
61 |
| - profile = user.profile |
62 |
| - profile.is_verified = True |
63 |
| - profile.save() |
64 |
| - |
65 |
| - tokens = RefreshToken.for_user(user) |
66 |
| - |
67 |
| - response = Response() |
68 |
| - response.data = { |
69 |
| - 'tokens': {'access': str(tokens.access_token), 'refresh': str(tokens)}, |
70 |
| - 'user_id': user.pk, |
71 |
| - 'username': user.username |
72 |
| - } |
73 |
| - |
74 |
| - response.status_code = 200 |
75 |
| - return response |
| 36 | + username = request.data.get('username') |
| 37 | + verification_code = request.data.get('verification_code') |
| 38 | + |
| 39 | + if not username or not verification_code: |
| 40 | + return Response(data={'message': 'Username and verification code are required!'}, status=400) |
| 41 | + |
| 42 | + user, error_response = Authenticator.authenticate_user(username, verification_code) |
| 43 | + if error_response: |
| 44 | + return error_response |
| 45 | + |
| 46 | + response_data = TokenGenerator.generate_tokens(user) |
| 47 | + return Response(data=response_data, status=200) |
76 | 48 |
|
77 | 49 |
|
78 | 50 | @api_view(['POST'])
|
@@ -105,5 +77,4 @@ def direct_42_login_page(request):
|
105 | 77 |
|
106 | 78 | @api_view(['POST'])
|
107 | 79 | def login_with_42(request, code):
|
108 |
| - response = api_42(code) |
109 |
| - return response |
| 80 | + return connect_api_42(code) |
0 commit comments