aboutsummaryrefslogtreecommitdiff
path: root/authentication
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-08-25 18:23:36 -0400
committerBobby <[email protected]>2024-08-25 18:23:36 -0400
commitc6c9c18c39b94cbb1db1c54a5993045c56e49c7e (patch)
tree5709a2caad01b3b2e12086a6af20ced2763dae81 /authentication
parentd846d0fa4483c9ea91e1082aac0f7aea9ed64653 (diff)
downloadyugen-c6c9c18c39b94cbb1db1c54a5993045c56e49c7e.tar.xz
yugen-c6c9c18c39b94cbb1db1c54a5993045c56e49c7e.zip
Better auth for SSR. Profile and Watch prep
Diffstat (limited to 'authentication')
-rw-r--r--authentication/urls.py1
-rw-r--r--authentication/views.py21
2 files changed, 14 insertions, 8 deletions
diff --git a/authentication/urls.py b/authentication/urls.py
index 7528b9c..f238dfa 100644
--- a/authentication/urls.py
+++ b/authentication/urls.py
@@ -5,4 +5,5 @@ from . import views
app_name = "auth"
urlpatterns = [
path("callback", views.callback, name="callback"),
+ path("logout", views.logout_user, name="logout"),
]
diff --git a/authentication/views.py b/authentication/views.py
index 6084371..a2b407b 100644
--- a/authentication/views.py
+++ b/authentication/views.py
@@ -1,25 +1,30 @@
-from django.http import JsonResponse
-from django.contrib.auth import login
-from authentication.utils import exchange_code, authenticate_user
-from django.shortcuts import redirect
+from django.http import HttpResponseRedirect
+from django.contrib.auth import login, logout
+from authentication.utils import exchange_code, authenticate_user, get_redirect_uri
+from django.shortcuts import redirect, render
def callback(request):
# Coming from Discord OAuth2
code = request.GET.get("code")
if not code:
- return JsonResponse({"error": "No code provided"})
+ return render(request, "messages/unauthorized.html", {"error": "You can't access the site if you keep cancelling the login!", "redirect_uri": get_redirect_uri()})
response = exchange_code(code=code)
if "error" in response:
- return JsonResponse(response)
+ return render(request, "messages/unauthorized.html", {"error": "You did something crazy, didn't you?", "redirect_uri": get_redirect_uri()})
user = authenticate_user(exchange_response=response)
if not user:
- return JsonResponse({"error": "User not authorized"})
+ return render(request, "messages/unauthorized.html", {"redirect_uri": get_redirect_uri()})
+ next_url = request.session.pop("next", None)
# login the user and redirect to the referrer
login(request, user)
- return redirect("home:index")
+ return redirect(next_url if next_url else "home:index")
+
+def logout_user(request):
+ logout(request)
+ return HttpResponseRedirect(request.META.get("HTTP_REFERER"))