Skip to content

Commit 9012091

Browse files
committed
.
1 parent a078bca commit 9012091

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

API/Apps/Auth/api/urls.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from django.urls import path
2-
from rest_framework_simplejwt.views import TokenRefreshView, TokenObtainPairView, TokenBlacklistView
2+
from rest_framework_simplejwt.views import TokenBlacklistView
33
from Apps.Auth.api.views import *
44
from rest_framework_simplejwt.views import TokenRefreshView
55

66
urlpatterns = [
77
path('register/', register, name='register'),
8-
path('login/', login, name='login'),
98
path('send-email-for-verification/', send_email_for_verification, name='email_verification'),
10-
path('email-verification/', email_verification, name='email_verification'),
9+
path('email-verification/', email_verification_and_login, name='email_verification'),
1110
path('change-password/', change_password, name='change_password'),
1211
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
1312
path('login-with-42/<str:code>/', login_with_42, name='login_with_42'),

API/Apps/Auth/api/views.py

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import threading
23

34
from django.http import HttpResponseRedirect
45
from pytz import timezone
@@ -23,23 +24,6 @@ def register(request):
2324
return Response(serializer.data, status=201)
2425

2526

26-
@api_view(['POST'])
27-
@permission_classes([IsEmailVerified])
28-
def login(request):
29-
user = User.objects.filter(username=request.data['username']).first()
30-
31-
refresh = RefreshToken.for_user(user)
32-
response = Response()
33-
response.set_cookie(key='jwt', value=str(refresh.access_token), httponly=True)
34-
response.data = {
35-
'tokens': {'access': str(refresh.access_token), 'refresh': str(refresh)},
36-
'user_id': user.pk,
37-
'username': user.username
38-
}
39-
response.status_code = 200
40-
return response
41-
42-
4327
@api_view(['POST'])
4428
@permission_classes([AllowAny])
4529
def send_email_for_verification(request):
@@ -48,13 +32,14 @@ def send_email_for_verification(request):
4832
user = User.objects.get(username=username)
4933
if user is None or not user.check_password(password):
5034
raise AuthenticationFailed("Wrong Password or Username!")
51-
send_email(user)
35+
thread = threading.Thread(target=send_email, args=[user])
36+
thread.start()
5237
return Response(data={'message': 'Email sent successfully!'}, status=200)
5338

5439

5540
@api_view(['POST'])
5641
@permission_classes([AllowAny])
57-
def email_verification(request):
42+
def email_verification_and_login(request):
5843
try:
5944
username = request.data['username']
6045
verification_code = request.data['verification_code']
@@ -69,6 +54,7 @@ def email_verification(request):
6954

7055
expiration_time = timedelta(minutes=15)
7156
if (datetime.now().astimezone(timezone('UTC')) - db_verification_code.expired_date) > expiration_time:
57+
db_verification_code.delete()
7258
return Response(data={'message': 'Verification code expired!'}, status=400)
7359
db_verification_code.delete()
7460

API/Apps/Auth/utils.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pyotp
24
from datetime import datetime, timedelta
35

@@ -28,12 +30,24 @@ def generate_otp():
2830

2931
def send_email(user):
3032
otp_code = generate_otp()
31-
receiver = user.email # test
32-
subject = 'LAST DANCE Email Verification'
33-
message = f'Hi! {user} your one-time verification code is {otp_code['otp']}'
34-
from_email = '[email protected]'
33+
receiver = user.email
34+
subject = 'Transcendence Email Verification'
35+
message = f'''
36+
Hello {user.username},
37+
38+
You can use the following one-time code to verify your Transcendence account:
39+
40+
Verification Code: {otp_code['otp']}
41+
42+
This code will help you securely verify your account.
43+
44+
Regards,
45+
Transcendence Team
46+
'''
47+
from_email = os.getenv("EMAIL_HOST_USER")
3548
recipient_list = [receiver]
3649
send_mail(subject, message, from_email, recipient_list)
3750

3851
VerificationCode.objects.create(code=otp_code['otp'], expired_date=otp_code['otp'], username=user)
3952

53+

0 commit comments

Comments
 (0)