Add: auth app

This commit is contained in:
Aroy-Art 2023-10-07 23:27:49 +02:00
parent ce13d4c5c5
commit ce1af464ed
Signed by: Aroy
GPG key ID: 583642324A1D2070
7 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class AuthConfig(AppConfig):
name = 'apps.auth'
label = 'apps_auth'

View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,51 @@
# Create your views here.
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from .forms import LoginForm, SignUpForm
def login_view(request):
form = LoginForm(request.POST or None)
msg = None
if request.method == "POST":
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect("/")
else:
msg = 'Invalid credentials'
else:
msg = 'Error validating the form'
return render(request, "accounts/login.html", {"form": form, "msg": msg})
def register_user(request):
msg = None
success = False
if request.method == "POST":
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get("username")
raw_password = form.cleaned_data.get("password1")
user = authenticate(username=username, password=raw_password)
msg = 'User created - please <a href="/login">login</a>.'
success = True
# return redirect("/login/")
else:
msg = 'Form is not valid'
else:
form = SignUpForm()
return render(request, "accounts/register.html", {"form": form, "msg": msg, "success": success})