This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
facturio/account/views.py

41 lines
1.3 KiB
Python
Raw Normal View History

2023-12-18 21:00:37 +01:00
from django.conf import settings
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.shortcuts import redirect, render
def auth_login(req: HttpRequest) -> HttpResponse:
if req.method == "POST":
username = req.POST["username"]
password = req.POST["password"]
user = authenticate(req, username=username, password=password)
if not user:
return JsonResponse(
{
"message": "Invalid cretendials"
},
status=401
)
else:
login(req, user)
redirect_url = getattr(req.GET, "next", settings.LOGIN_REDIRECT_URL)
return redirect(redirect_url)
elif req.method == "GET":
return render(req, "account/login.html")
return HttpResponse(status=405)
@login_required
def auth_logout(req: HttpRequest) -> HttpResponse:
logout(req)
return redirect(settings.LOGOUT_REDIRECT_URL)
def auth_register(req: HttpRequest) -> HttpResponse:
return render(req, "account/register.html")
@login_required
def me(req: HttpRequest) -> HttpResponse:
return render(req, "account/me.html")