From 1ba8f68a0e9a87c3005d50473f38548938a627ed Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 23 Jun 2024 11:33:56 +0200 Subject: [PATCH] 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. --- freedoi/accounts/urls.py | 5 ++--- freedoi/accounts/views.py | 8 +++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/freedoi/accounts/urls.py b/freedoi/accounts/urls.py index c664fc9..ba0bdaa 100644 --- a/freedoi/accounts/urls.py +++ b/freedoi/accounts/urls.py @@ -1,7 +1,6 @@ from django.urls import path -from django.contrib.auth import views as auth_views from django.views.generic import TemplateView -from .views import SendLoginEmailView, LoginView +from .views import SendLoginEmailView, LoginView, LogoutView urlpatterns = [ path("login///", LoginView.as_view(), name="login"), @@ -10,7 +9,7 @@ urlpatterns = [ SendLoginEmailView.as_view(), name="login", ), - path("logout/", auth_views.LogoutView.as_view(next_page="/"), name="logout"), + path("logout/", LogoutView.as_view(), name="logout"), path( "email-sent/", TemplateView.as_view(template_name="accounts/email_sent.html"), diff --git a/freedoi/accounts/views.py b/freedoi/accounts/views.py index c4d85a6..9b1d933 100644 --- a/freedoi/accounts/views.py +++ b/freedoi/accounts/views.py @@ -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.utils.http import urlsafe_base64_encode, urlsafe_base64_decode from django.utils.encoding import force_bytes, force_str -from django.template.loader import render_to_string from django.http import HttpResponse from django.shortcuts import redirect from django.views.generic import FormView, View from django.contrib.auth.tokens import default_token_generator +from django.contrib.auth import logout from .forms import EmailForm User = get_user_model() @@ -48,3 +48,9 @@ class LoginView(View): return redirect("home") else: return HttpResponse("Login link is invalid") + + +class LogoutView(View): + def get(self, request): + logout(request) + return redirect("home") \ No newline at end of file