Commit 24dcbd38 authored by gab's avatar gab

created accounts app for logins

parent 56cde85d
from django.apps import AppConfig
class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
class EmailBackend(ModelBackend):
def authenticate(self, request, email, password, **kwargs):
UserModel = get_user_model()
try:
user = UserModel.objects.get(email=email)
except UserModel.DoesNotExist:
return None
else:
if user.check_password(password):
return user
return None
\ No newline at end of file
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from .models import CustomUser
from django import forms
from django.forms.widgets import PasswordInput, EmailInput
# Authenticate a User // Login
class CustomUserAuthenticationForm(AuthenticationForm):
email = forms.EmailField(widget=forms.EmailInput(attrs={'autofocus': True}))
password = forms.CharField(widget=PasswordInput())
def __init__(self, *args, **kwargs):
super(CustomUserAuthenticationForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
self.fields.pop('username')
self.order_fields(['email', 'password'])
# Registering a User Form
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ["email", "first_name", "last_name", "password1", "password2"]
# class CustomUserChangeForm(UserChangeForm):
# class Meta:
# model = CustomUser
# fields = ("username", "email")
\ No newline at end of file
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
username = None
email = models.EmailField(unique=True, db_index=True)
first_name = models.CharField(blank=False, max_length=150)
last_name = models.CharField(blank=False, max_length=150)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.first_name
\ No newline at end of file
{% extends "base.html" %}
{% block content %}
<form method="POST" autocomplete="off">
{% csrf_token %}
{{ loginform.as_p }}
<input type="submit" value="Log In"/>
</form>
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% block content %}
<form method="POST" autocomplete="off">
{% csrf_token %}
{{ registerform.as_p }}
<input type="submit" value="Register"/>
</form>
{% endblock %}
\ No newline at end of file
from django.test import TestCase
# Create your tests here.
from django.urls import path
from . import views
app_name = "accounts"
urlpatterns = [
path('login', views.login_page, name="login"),
path('register', views.register, name="register"),
path('logout', views.user_logout, name="logout")
]
\ No newline at end of file
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .forms import CustomUserCreationForm, CustomUserAuthenticationForm
from django.contrib.auth.models import auth
from django.contrib.auth import authenticate, login, logout
def register(request):
# return HttpResponse("I am in Register")
form = CustomUserCreationForm()
if request.method == "POST":
form = CustomUserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect("index")
context = {
'registerform': form,
}
return render(request, 'accounts/register.html', context = context)
def login_page(request):
form = CustomUserAuthenticationForm()
if request.method == "POST":
form = CustomUserAuthenticationForm(request, data = request.POST)
if form.is_valid():
email = request.POST.get('email')
password = request.POST.get('password')
user = authenticate(request, email = email, password = password)
if user is not None:
auth.login(request, user)
return redirect("index")
context = {
'loginform' : form
}
return render(request, 'accounts/login.html', context = context)
def user_logout(request):
auth.logout(request)
return redirect("index")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment