Skip to content

Commit

Permalink
Ensure username change persistence in serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel81 committed Mar 17, 2024
1 parent b03c2cd commit 127e94f
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions backend/accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

class UserSerializer(serializers.ModelSerializer):
load_dotenv()
profile_picture = serializers.SerializerMethodField()

class Meta:
model = User
fields = ('id', 'username', 'email', 'profile_picture',)

def get_profile_picture(self, obj):
return f"https://res.cloudinary.com/{os.getenv('CLOUDINARY_NAME')}/{obj.profile_picture}"
if obj.profile_picture:
return f"https://res.cloudinary.com/{os.getenv('CLOUDINARY_NAME')}/{obj.profile_picture}"
return None


def get_tokens_for_user(user):
refresh = RefreshToken.for_user(user)
Expand All @@ -39,18 +41,19 @@ def validate_code(self, code):
if access_token:
user_data = Github.get_github_user(access_token)
github_username = user_data['login']
github_id = user_data['id']
emails_data = Github.get_github_emails(access_token)
primary_email = next((email for email in emails_data if email.get('primary')), None)

if primary_email:
email = primary_email.get('email')

try:
user = User.objects.get(username=github_username)
user = User.objects.get(github_id=github_id)
user.email = email
user.save()
except User.DoesNotExist:
user = User.objects.create_user(username=github_username, email=email)
user = User.objects.create_user(username=user_data['login'], email=email, github_id=github_id)

serialized_user = UserSerializer(user).data

Expand All @@ -62,13 +65,6 @@ def validate_code(self, code):
serialized_user['access_token'] = access_token
serialized_user['refresh_token'] = refresh_token

# return {
# 'access_token': access_token,
# 'refresh_token': refresh_token,
# 'user_id': user.id,
# 'username': github_username,
# 'email': email,
# }
return serialized_user
else:
raise serializers.ValidationError("Unable to fetch access token from GitHub.")

0 comments on commit 127e94f

Please sign in to comment.