Skip to content

Commit 2044950

Browse files
committed
Login Fixes
1 parent 5eb183c commit 2044950

File tree

3 files changed

+74
-55
lines changed

3 files changed

+74
-55
lines changed

backend/api/utils/email_sync.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from api.core import logger
2+
from api.models import (
3+
Admin,
4+
Guest,
5+
Hub,
6+
MentorProfile,
7+
MenteeProfile,
8+
MentorApplication,
9+
MenteeApplication,
10+
NewMentorApplication,
11+
PartnerApplication,
12+
PartnerProfile,
13+
Support,
14+
Moderator,
15+
Users,
16+
VerifiedEmail,
17+
)
18+
19+
20+
def update_email_across_models(old_email: str, new_email: str) -> None:
21+
if not old_email or not new_email:
22+
return
23+
old_email_normalized = old_email.strip()
24+
new_email_normalized = new_email.strip().lower()
25+
if old_email_normalized.lower() == new_email_normalized:
26+
return
27+
28+
models = [
29+
MenteeApplication,
30+
MentorApplication,
31+
NewMentorApplication,
32+
PartnerApplication,
33+
MenteeProfile,
34+
MentorProfile,
35+
PartnerProfile,
36+
Users,
37+
VerifiedEmail,
38+
Guest,
39+
Support,
40+
Moderator,
41+
Admin,
42+
Hub,
43+
]
44+
45+
for model in models:
46+
try:
47+
model.objects(email__iexact=old_email_normalized).update(
48+
set__email=new_email_normalized
49+
)
50+
except Exception as exc:
51+
logger.error(
52+
f"Failed updating email for {model.__name__} {old_email_normalized} -> {new_email_normalized}: {exc}"
53+
)

backend/api/views/admin.py

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from api.utils.require_auth import admin_only
2323
from api.utils.request_utils import get_profile_model, imgur_client
2424
from api.utils.constants import Account
25+
from api.utils.email_sync import update_email_across_models
2526
import csv
2627
import io
2728
from api.views.auth import create_firebase_user
@@ -433,60 +434,6 @@ def editEmailPassword():
433434
email=email,
434435
)
435436
if email != ex_email:
436-
ex_data = MenteeApplication.objects.filter(email=ex_email)
437-
if len(ex_data) > 0:
438-
for ex_item in ex_data:
439-
ex_item.email = email
440-
ex_item.save()
441-
ex_data = MentorApplication.objects.filter(email=ex_email)
442-
if len(ex_data) > 0:
443-
for ex_item in ex_data:
444-
ex_item.email = email
445-
ex_item.save()
446-
ex_data = MenteeProfile.objects.filter(email=ex_email)
447-
if len(ex_data) > 0:
448-
for ex_item in ex_data:
449-
ex_item.email = email
450-
ex_item.save()
451-
ex_data = MentorProfile.objects.filter(email=ex_email)
452-
if len(ex_data) > 0:
453-
for ex_item in ex_data:
454-
ex_item.email = email
455-
ex_item.save()
456-
ex_data = NewMentorApplication.objects.filter(email=ex_email)
457-
if len(ex_data) > 0:
458-
for ex_item in ex_data:
459-
ex_item.email = email
460-
ex_item.save()
461-
ex_data = PartnerProfile.objects.filter(email=ex_email)
462-
if len(ex_data) > 0:
463-
for ex_item in ex_data:
464-
ex_item.email = email
465-
ex_item.save()
466-
ex_data = Users.objects.filter(email=ex_email)
467-
if len(ex_data) > 0:
468-
for ex_item in ex_data:
469-
ex_item.email = email
470-
ex_item.save()
471-
ex_data = VerifiedEmail.objects.filter(email=ex_email)
472-
if len(ex_data) > 0:
473-
for ex_item in ex_data:
474-
ex_item.email = email
475-
ex_item.save()
476-
ex_data = Guest.objects.filter(email=ex_email)
477-
if len(ex_data) > 0:
478-
for ex_item in ex_data:
479-
ex_item.email = email
480-
ex_item.save()
481-
ex_data = Support.objects.filter(email=ex_email)
482-
if len(ex_data) > 0:
483-
for ex_item in ex_data:
484-
ex_item.email = email
485-
ex_item.save()
486-
ex_data = Moderator.objects.filter(email=ex_email)
487-
if len(ex_data) > 0:
488-
for ex_item in ex_data:
489-
ex_item.email = email
490-
ex_item.save()
437+
update_email_across_models(ex_email, email)
491438

492439
return create_response(status=200, message="successful edited")

backend/api/views/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
)
4848
from api.utils.constants import NEW_APPLICATION_STATUS
4949
from api.utils.profile_parse import new_profile, edit_profile
50+
from api.utils.email_sync import update_email_across_models
5051
from api.utils.constants import Account
5152
from api.utils.require_auth import all_users, mentee_only, verify_user
5253
from firebase_admin import auth as firebase_admin_auth
@@ -931,9 +932,27 @@ def edit_mentor(id):
931932
logger.info(msg)
932933
return create_response(status=422, message=msg)
933934

935+
old_email = account.email.strip().lower() if account.email else account.email
936+
934937
if not edit_profile(data, account):
935938
msg = "Couldn't update profile"
936939
return create_response(status=500, message=msg)
940+
new_email = account.email.strip().lower() if account.email else account.email
941+
if new_email and old_email and new_email != old_email:
942+
try:
943+
if account.firebase_uid:
944+
firebase_admin_auth.update_user(account.firebase_uid, email=new_email)
945+
else:
946+
firebase_user = firebase_admin_auth.get_user_by_email(old_email)
947+
firebase_admin_auth.update_user(firebase_user.uid, email=new_email)
948+
except Exception as exc:
949+
logger.error(
950+
f"Failed to update Firebase email {old_email} -> {new_email}: {exc}"
951+
)
952+
return create_response(status=422, message="Email update failed")
953+
954+
update_email_across_models(old_email, new_email)
955+
account.email = new_email
937956

938957
account.save()
939958

0 commit comments

Comments
 (0)