Skip to content

Commit c8d6f37

Browse files
github login fix
1 parent 82bca74 commit c8d6f37

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

accounts/auth.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ def generate_random_password(
2828
)
2929

3030

31-
def extract_email(response_data):
32-
email = ''
31+
def extract_email(response_data, sociallogin=None):
32+
if response_data.get("email"):
33+
return response_data["email"]
3334

34-
if 'email' in response_data:
35-
email = response_data['email']
35+
# GitHub-specific (via allauth fetch)
36+
if sociallogin and sociallogin.email_addresses:
37+
verified = [e for e in sociallogin.email_addresses if e.verified]
38+
if verified:
39+
return verified[0].email
40+
return sociallogin.email_addresses[0].email
3641

37-
if 'user' in response_data:
38-
user_data = response_data['user']
39-
if 'email' in user_data:
40-
email = user_data['email']
42+
if response_data.get("notification_email"):
43+
return response_data["notification_email"]
4144

42-
return email.lower().strip()
45+
raise KeyError("Email not found in response data")
4346

4447

4548
class ConsolidatingSocialAccountAdapter(DefaultSocialAccountAdapter):
@@ -55,7 +58,7 @@ def pre_social_login(self, request, sociallogin):
5558

5659
email = None
5760
try:
58-
email = extract_email(response_data)
61+
email = extract_email(response_data, sociallogin=sociallogin)
5962
logger.info(f"Extracted email {email} from OAuth response")
6063
if email is None or len(email) == 0:
6164
logger.info("No email found in social account data")

accounts/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class CustomUser(AbstractUser):
1919
help_text="Your actual name that will appear on your certificates"
2020
)
2121

22+
def __str__(self):
23+
# safest is to display something stable
24+
return self.username or self.email or str(self.pk)
25+
2226

2327
class Token(models.Model):
2428
key = models.CharField(max_length=40, primary_key=True)

course_management/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@
305305
# Skip intermediate confirmation page - direct OAuth redirect
306306
SOCIALACCOUNT_LOGIN_ON_GET = True
307307

308+
SOCIALACCOUNT_PROVIDERS = {
309+
"github": {
310+
"SCOPE": [
311+
"user:email",
312+
],
313+
"VERIFIED_EMAIL": True,
314+
}
315+
}
308316

309317
# Unfold Configurations
310318
UNFOLD = {

0 commit comments

Comments
 (0)