From 835f8a3c3d3d83e6b0ad545f58ef2eb62b5366dc Mon Sep 17 00:00:00 2001 From: Bobby Date: Fri, 29 Aug 2025 11:18:42 +0530 Subject: migrate auth to authentication; fix asgi and settings --- authentication/__init__.py | 0 authentication/admin.py | 3 +++ authentication/apps.py | 6 ++++++ authentication/migrations/__init__.py | 0 authentication/models.py | 3 +++ authentication/tests.py | 3 +++ authentication/urls.py | 8 ++++++++ authentication/views.py | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 57 insertions(+) create mode 100644 authentication/__init__.py create mode 100644 authentication/admin.py create mode 100644 authentication/apps.py create mode 100644 authentication/migrations/__init__.py create mode 100644 authentication/models.py create mode 100644 authentication/tests.py create mode 100644 authentication/urls.py create mode 100644 authentication/views.py (limited to 'authentication') diff --git a/authentication/__init__.py b/authentication/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/authentication/admin.py b/authentication/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/authentication/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/authentication/apps.py b/authentication/apps.py new file mode 100644 index 00000000..c65f1d28 --- /dev/null +++ b/authentication/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AuthenticationConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "authentication" diff --git a/authentication/migrations/__init__.py b/authentication/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/authentication/models.py b/authentication/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/authentication/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/authentication/tests.py b/authentication/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/authentication/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/authentication/urls.py b/authentication/urls.py new file mode 100644 index 00000000..d49f2956 --- /dev/null +++ b/authentication/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +app_name = "authentication" +urlpatterns = [ + path("/login", views.login, name="login"), + path("/logout", views.logout, name="logout"), +] diff --git a/authentication/views.py b/authentication/views.py new file mode 100644 index 00000000..9921b17e --- /dev/null +++ b/authentication/views.py @@ -0,0 +1,34 @@ +from django.contrib import messages +from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout +from django.shortcuts import redirect +from users.functions import email_verified + +def login(request): + next = request.POST.get("next", "core:home").strip() + username = request.POST.get("username") + password = request.POST.get("password") + + print("Next:", next) + print("Username:", username) + print("Password:", password) + if username == "" or password == "" or username is None or password is None: + messages.error(request, "ErrorEmptyFields", extra_tags="LoginError") + return redirect(f"{next}?username={username}" if username else next) + else: + user = authenticate(request, username=username, password=password) + if user is not None: + if email_verified(user): + auth_login(request, user) + return redirect(next) + else: + messages.error(request, "ErrorEmailNotVerified", extra_tags="LoginError") + return redirect(f"{next}?username={username}") + else: + messages.error(request, "ErrorInvalidCredentials", extra_tags="LoginError") + return redirect(f"{next}?username={username}") + + +def logout(request): + auth_logout(request) + referer = request.META.get('HTTP_REFERER', '/') + return redirect(referer) -- cgit v1.2.3