Skip to content

Commit

Permalink
Fix error on login without email address
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
NicholasBottone committed Jul 18, 2024
1 parent 96311d3 commit c7cbe94
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
6 changes: 5 additions & 1 deletion discordoauth2/auth.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from django.contrib.auth.backends import RemoteUserBackend
from .models import User
from typing import Union


class DiscordAuthenticationBackend(RemoteUserBackend):
def authenticate(self, request, user) -> User:
def authenticate(self, request, user) -> Union[User, None]:
found_user = User.objects.filter(id=user['id'])
if len(found_user) == 0:
# New user (first time login)
if user['email'] is None:
return None
new_user = User.objects.create_discord_user(user)
return new_user
# Returning user
Expand Down
14 changes: 12 additions & 2 deletions discordoauth2/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union
from django.http import JsonResponse, HttpRequest
from django.shortcuts import redirect, render
from django.contrib.auth import authenticate, login
Expand Down Expand Up @@ -36,20 +37,28 @@ def discord_api_login(request: HttpRequest):

def discord_login_redirect(request: HttpRequest):
user = exchange_code(request.GET.get('code'))
if user is None:
return redirect('/login-error/')
discord_user = authenticate(request, user=user)
if discord_user is None:
return redirect('/login-error/')
login(request, discord_user)
return redirect('/')


def discord_api_login_redirect(request: HttpRequest):
user = exchange_code(request.GET.get(
'code'), redirect_uri=DISCORD_API_REDIRECT_URI)
if user is None:
return redirect('/login-error/')
discord_user = authenticate(request, user=user)
if discord_user is None:
return redirect('/login-error/')
login(request, discord_user)
return redirect('/api/highscores/auth/')


def exchange_code(code: str, redirect_uri: str = DISCORD_REDIRECT_URI) -> requests.Response:
def exchange_code(code: str, redirect_uri: str = DISCORD_REDIRECT_URI) -> Union[requests.Response, None]:
data = {
'client_id': DISCORD_CLIENT_ID,
'client_secret': DISCORD_CLIENT_SECRET,
Expand All @@ -64,7 +73,8 @@ def exchange_code(code: str, redirect_uri: str = DISCORD_REDIRECT_URI) -> reques

response = requests.post('%s/oauth2/token' %
DISCORD_API_ENDPOINT, data=data, headers=headers)
response.raise_for_status()
if response.status_code != 200:
return None

response = requests.get('%s/users/@me' % DISCORD_API_ENDPOINT, headers={
'Authorization': 'Bearer %s' % response.json()['access_token']
Expand Down
17 changes: 17 additions & 0 deletions home/templates/home/login_error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'home/base.html' %} {% block content %}
<div
class="
position-relative
overflow-hidden
p-3 p-md-5
m-md-3
text-center
bg-danger
"
>
<div class="col-md-5 p-lg-5 mx-auto my-5">
<h1 class="display-4 fw-normal">Login error</h1>
<p>Ensure there is an email address attached to your discord account, and you select authorize.</p>
</div>
</div>
{% endblock %}
1 change: 1 addition & 0 deletions home/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
path("privacy/", views.privacy, name="privacy"),
path("logopack/", views.logos, name="logos"),
path("link-success/", views.link_success, name="link success"),
path("login-error/", views.login_error, name="login error"),
]
4 changes: 4 additions & 0 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def login_page(request):
return redirect('/oauth2/login')


def login_error(request):
return render(request, "home/login_error.html", {})


def logout_user(request):
logout(request)
return redirect('/')
Expand Down

0 comments on commit c7cbe94

Please sign in to comment.