feat(auth): refactor logout handling to custom view

Replaced Django's built-in LogoutView with a custom LogoutView. This change includes implementing the custom view in place of the previous auth_views.LogoutView. The custom view directly logs out the user and redirects to the home page.

This refactor allows greater flexibility and control over the logout process, potentially enabling additional custom logic to be added in future.
This commit is contained in:
Kumi 2024-06-23 11:33:56 +02:00
parent ce8d22a043
commit 1ba8f68a0e
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 9 additions and 4 deletions

View file

@ -1,7 +1,6 @@
from django.urls import path from django.urls import path
from django.contrib.auth import views as auth_views
from django.views.generic import TemplateView from django.views.generic import TemplateView
from .views import SendLoginEmailView, LoginView from .views import SendLoginEmailView, LoginView, LogoutView
urlpatterns = [ urlpatterns = [
path("login/<uidb64>/<token>/", LoginView.as_view(), name="login"), path("login/<uidb64>/<token>/", LoginView.as_view(), name="login"),
@ -10,7 +9,7 @@ urlpatterns = [
SendLoginEmailView.as_view(), SendLoginEmailView.as_view(),
name="login", name="login",
), ),
path("logout/", auth_views.LogoutView.as_view(next_page="/"), name="logout"), path("logout/", LogoutView.as_view(), name="logout"),
path( path(
"email-sent/", "email-sent/",
TemplateView.as_view(template_name="accounts/email_sent.html"), TemplateView.as_view(template_name="accounts/email_sent.html"),

View file

@ -2,11 +2,11 @@ from django.contrib.auth import get_user_model, login as auth_login
from django.core.mail import send_mail from django.core.mail import send_mail
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.utils.encoding import force_bytes, force_str from django.utils.encoding import force_bytes, force_str
from django.template.loader import render_to_string
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import redirect from django.shortcuts import redirect
from django.views.generic import FormView, View from django.views.generic import FormView, View
from django.contrib.auth.tokens import default_token_generator from django.contrib.auth.tokens import default_token_generator
from django.contrib.auth import logout
from .forms import EmailForm from .forms import EmailForm
User = get_user_model() User = get_user_model()
@ -48,3 +48,9 @@ class LoginView(View):
return redirect("home") return redirect("home")
else: else:
return HttpResponse("Login link is invalid") return HttpResponse("Login link is invalid")
class LogoutView(View):
def get(self, request):
logout(request)
return redirect("home")