diff --git a/src/discord_auth.py b/src/discord_auth.py index c2c3995..4b95861 100644 --- a/src/discord_auth.py +++ b/src/discord_auth.py @@ -71,9 +71,9 @@ def discord_oauth_callback(): if user_json: # lookup by email user = Users.query.filter_by(email=user_json["email"]).first() + discord_user = DiscordUser.query.filter_by(id=user_json["id"]).first() if user is None: # Check if user changed email - discord_user = DiscordUser.query.filter_by(id=user_json["id"]).first() if discord_user: user = Users.query.filter_by(email=discord_user.email) if user: @@ -105,6 +105,20 @@ def discord_oauth_callback(): db.session.add(user) db.session.add(discord_user) db.session.commit() + else: + # Create Discord association if does not exist (legacy support) + if not discord_user: + discord_user = DiscordUser( + id=user_json["id"], + username=user_json["username"], + discriminator=user_json["discriminator"], + avatar_hash=user_json["avatar"], + mfa_enabled=user_json["mfa_enabled"], + verified=user_json["verified"], + email=user_json["email"] + ) + db.session.add(discord_user) + db.session.commit() # Login login_user(user) else: