41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
|
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")
|