Skip to content

Commit 75115a9

Browse files
authored
Merge pull request #272 from AI4Bharat/anudesh-login-fix
Anudesh login fix
2 parents 68640ca + 8f1c5ac commit 75115a9

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

backend/users/views.py

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -627,27 +627,61 @@ def login(self, request, *args, **kwargs):
627627
# {"message": "Incorrect Password."}, status=status.HTTP_400_BAD_REQUEST
628628
# )
629629

630+
from django.contrib.auth.hashers import check_password
631+
632+
is_guest_user = getattr(user, 'guest_user', True)
633+
634+
if is_guest_user:
635+
return Response(
636+
{"message": "This account is a guest user. Please use Google login."},
637+
status=status.HTTP_400_BAD_REQUEST,
638+
)
639+
640+
# For non-guest users, check local password
641+
if hasattr(user, 'password') and user.password and check_password(password, user.password):
642+
refresh = RefreshToken.for_user(user)
643+
access_token = str(refresh.access_token)
644+
refresh_token = str(refresh)
645+
return Response(
646+
{
647+
"message": "Logged in successfully.",
648+
"refresh": refresh_token,
649+
"access": access_token,
650+
},
651+
status=status.HTTP_200_OK,
652+
)
653+
630654
try:
631655
firebase = pyrebase.initialize_app(config)
632656
auth = firebase.auth()
633657
auth.sign_in_with_email_and_password(email, password)
658+
print("Firebase authentication successful")
634659
refresh = RefreshToken.for_user(user)
635660
access_token = str(refresh.access_token)
636661
refresh_token = str(refresh)
637-
except:
662+
638663
return Response(
639-
{"message": "Authentication failed."},
640-
status=status.HTTP_400_BAD_REQUEST,
664+
{
665+
"message": "Logged in successfully.",
666+
"refresh": refresh_token,
667+
"access": access_token,
668+
},
669+
status=status.HTTP_200_OK,
641670
)
671+
except Exception as e:
672+
if hasattr(user, 'password') and user.password:
673+
# User has local password but it didn't match
674+
return Response(
675+
{"message": "Incorrect password."},
676+
status=status.HTTP_400_BAD_REQUEST,
677+
)
678+
else:
679+
# User doesn't have local password
680+
return Response(
681+
{"message": "Authentication failed. Please check your credentials."},
682+
status=status.HTTP_400_BAD_REQUEST,
683+
)
642684

643-
return Response(
644-
{
645-
"message": "Logged in successfully.",
646-
"refresh": refresh_token,
647-
"access": access_token,
648-
},
649-
status=status.HTTP_200_OK,
650-
)
651685

652686
@permission_classes([AllowAny])
653687
@action(
@@ -725,7 +759,12 @@ def google_login(self, request, *args, **kwargs):
725759

726760
try:
727761
user = User.objects.get(email=email)
728-
except:
762+
user.first_name = fName
763+
user.last_name = lName
764+
user.profile_photo = photoUrl
765+
user.save()
766+
767+
except User.DoesNotExist:
729768
user = User(
730769
username=str(email).split("@")[0],
731770
email=email.lower(),
@@ -744,9 +783,9 @@ def google_login(self, request, *args, **kwargs):
744783
refresh = RefreshToken.for_user(user)
745784
access_token = str(refresh.access_token)
746785
refresh_token = str(refresh)
747-
except:
786+
except Exception as e:
748787
return Response(
749-
{"message": "Token generation failed."},
788+
{"message": "Token generation failed.", "error": str(e)},
750789
status=status.HTTP_400_BAD_REQUEST,
751790
)
752791

@@ -1148,6 +1187,17 @@ def user_details_update(self, request, pk=None):
11481187
)
11491188
user = User.objects.get(id=pk)
11501189
serializer = UserUpdateSerializer(user, request.data, partial=True)
1190+
existing_guest_user = getattr(user, 'guest_user', True)
1191+
new_guest_user = request.data.get("guest_user", None)
1192+
1193+
password_reset_sent = False
1194+
if existing_guest_user is True and new_guest_user is False:
1195+
try:
1196+
firebase = pyrebase.initialize_app(config)
1197+
auth = firebase.auth()
1198+
auth.send_password_reset_email(user.email)
1199+
password_reset_sent = True
1200+
except Exception as e:
11511201

11521202
existing_is_active = user.is_active
11531203
is_active_payload = request.data.get("is_active", None)

0 commit comments

Comments
 (0)