Authentication frontend update

This commit is contained in:
Piotr Kopycki 2021-12-20 19:40:24 +01:00
parent ba37eeb6f4
commit 0f52b54a2e
10 changed files with 112 additions and 21 deletions

View File

@ -116,9 +116,9 @@ REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": ( "DEFAULT_PERMISSION_CLASSES": (
"rest_framework.permissions.IsAuthenticated", "rest_framework.permissions.IsAuthenticated",
), ),
# "DEFAULT_AUTHENTICATION_CLASSES": ( # "DEFAULT_AUTHENTICATION_CLASSES": (
# "rest_framework_simplejwt.authentication.JWTAuthentication", # "rest_framework_simplejwt.authentication.JWTAuthentication",
# ), # ),
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination", "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"DEFAULT_FILTER_BACKENDS": ( "DEFAULT_FILTER_BACKENDS": (
"django_filters.rest_framework.DjangoFilterBackend", "django_filters.rest_framework.DjangoFilterBackend",
@ -207,3 +207,6 @@ CORS_ALLOW_HEADERS = ('content-disposition', 'accept-encoding',
'content-type', 'accept', 'origin', 'authorization') 'content-type', 'accept', 'origin', 'authorization')
MEDIA_URL = "/media/" MEDIA_URL = "/media/"
LOGIN_URL='welcome'
LOGIN_REDIRECT_URL='welcome'

View File

@ -16,10 +16,11 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.urls import include from django.urls import include
from django.urls import path from django.urls import path
from .views import index from .views import home, welcome
urlpatterns = [ urlpatterns = [
path('', index), path('', welcome, name='welcome'),
path('home', home, name='home'),
path('users/', include("users.urls")), path('users/', include("users.urls")),
path('questions/', include("questions.urls")), path('questions/', include("questions.urls")),
path('answers/', include("answers.urls")), path('answers/', include("answers.urls")),

View File

@ -1,9 +1,15 @@
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.template import loader from django.template import loader
from django.contrib.auth.decorators import login_required
def index(request): @login_required
def home(request):
# context = { # context = {
# 'latest_question_list': latest_question_list, # 'latest_question_list': latest_question_list,
# } # }
return render(request, 'index.html') return render(request, 'home.html')
def welcome(request):
return render(request, 'welcome.html')

View File

@ -23,6 +23,10 @@
color: #00916E; color: #00916E;
} }
.sidenav-footer {
}
.main { .main {
margin-left: 160px; margin-left: 160px;
padding: 0px 10px; padding: 0px 10px;
@ -51,6 +55,12 @@
text-transform: uppercase; text-transform: uppercase;
} }
.authContent h3 {
margin: 0;
padding: 0 0 20px;
text-align: center;
}
.authContent p { .authContent p {
margin: 0; margin: 0;
padding: 0; padding: 0;
@ -106,4 +116,18 @@ input[type=checkbox]{
display: block; display: block;
} }
.authContent button{
width: 100%;
margin-bottom: 20px;
height: 30px;
color: #FFF;
font-size: 15px;
background: #00916E;
cursor: pointer;
border-radius: 25px;
border: none;
outline: none;
margin-top: 15px;
}
background-color:#FA003F background-color:#FA003F

21
templates/login2.html Normal file
View File

@ -0,0 +1,21 @@
{% extends "authBase.html" %}
{% block title %}Login{% endblock %}
{% block content %}
<h2>Log In</h2>
<form method="post"> {% csrf_token %}
{% for field in login_form %}
<p>
{{field.label_tag}}
{{field}}
{% for error in field.errors %}
<p style="color: red;">{{error}}</p>
{% endfor %}
</p>
{% endfor %}
<input type="submit" value="Login">
</form>
{% endblock %}

11
templates/welcome.html Normal file
View File

@ -0,0 +1,11 @@
{% extends "authBase.html" %}
{% block title %}Super Original and Innovative Test App{% endblock %}
{% block content %}
<h2>Super Original and Innovative Test App</h2>
<h3>Get started now</h3>
<button onclick="location.href = 'users/login'">Already have an account</button>
<button onclick="location.href = 'users/register'">Create new account</button>
{% endblock %}

View File

@ -1,5 +1,5 @@
from django import forms from django import forms
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UsernameField
from .models import User from .models import User
class RegistrationForm(UserCreationForm): class RegistrationForm(UserCreationForm):
@ -11,4 +11,13 @@ class RegistrationForm(UserCreationForm):
class Meta: class Meta:
model = User model = User
fields = ("email", "first_name", "last_name", "password1", "password2") fields = ("email", "first_name", "last_name", "password1", "password2")
class LoginForm(AuthenticationForm):
#email = forms.EmailField(max_length=60, widget=forms.EmailInput(attrs={'placeholder': 'Enter Email'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Enter Password'}))
class Meta:
model = User
fields = ("email", "password")

View File

@ -4,7 +4,7 @@ from django.urls import path
from users.views import UserModelViewSet from users.views import UserModelViewSet
from rest_framework_simplejwt.views import TokenObtainPairView from rest_framework_simplejwt.views import TokenObtainPairView
from rest_framework_simplejwt.views import TokenRefreshView from rest_framework_simplejwt.views import TokenRefreshView
from .views import PasswordReset, UserPasswordResetConfirmView, RegisterViewSet, login, logout, register, register_success, loginn, login_success from .views import PasswordReset, UserPasswordResetConfirmView, RegisterViewSet, login, logout, register, register_success, login2
router = DefaultRouter(trailing_slash=False) router = DefaultRouter(trailing_slash=False)
@ -13,10 +13,10 @@ router.register("items", UserModelViewSet)
urlpatterns = [ urlpatterns = [
path("", include(router.urls)), path("", include(router.urls)),
path('login', loginn, name='login'), path('login', login, name='login'),
path('logout', logout, name='logout'), path('logout', logout, name='logout'),
path('register/success', register_success, name='register_success'), path('register/success', register_success, name='register_success'),
path('login/success', login_success, name='login_success'), #path('login/success', login_success, name='login_success'),
# path('register', RegisterViewSet.as_view(), name='register'), # path('register', RegisterViewSet.as_view(), name='register'),
path('register', register, name='register'), path('register', register, name='register'),
path('api/token', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token', TokenObtainPairView.as_view(), name='token_obtain_pair'),

View File

@ -10,7 +10,7 @@ from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSe
from rest_framework.response import Response from rest_framework.response import Response
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.template import loader from django.template import loader
from .forms import RegistrationForm from .forms import RegistrationForm, LoginForm
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login as auth_login from django.contrib.auth import login as auth_login
from config.authh import SettingsBackend from config.authh import SettingsBackend
@ -70,12 +70,6 @@ class UserPasswordResetConfirmView(PasswordResetConfirmShortcut, generics.Generi
token_generator = default_token_generator token_generator = default_token_generator
def login(request):
# context = {
# 'latest_question_list': latest_question_list,
# }
return render(request, 'login.html')
def logout(request): def logout(request):
# context = { # context = {
# 'latest_question_list': latest_question_list, # 'latest_question_list': latest_question_list,
@ -109,7 +103,29 @@ def login_success(request):
return render(request, 'great.html') return render(request, 'great.html')
def loginn(request): def login2(request):
context = {}
if request.POST:
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data["email"],
password = form.cleaned_data["password1"]
# username = request.POST.get("email")
# password = request.POST.get("password")
user = SettingsBackend().authenticate(request, email=email, password=password)
if user is not None:
auth_login(request, user)
return redirect('home')
else:
context['login_form'] = form
else:
form = LoginForm()
context['login_form'] = form
return render(request, 'login2.html', context)
def login(request):
context = {}
if request.POST: if request.POST:
username = request.POST.get("username") username = request.POST.get("username")
password = request.POST.get("password") password = request.POST.get("password")
@ -117,5 +133,5 @@ def loginn(request):
user = SettingsBackend().authenticate(request, email=username, password=password) user = SettingsBackend().authenticate(request, email=username, password=password)
if user is not None: if user is not None:
auth_login(request, user) auth_login(request, user)
context = {} return redirect('home')
return render(request, 'login.html', context) return render(request, 'login.html', context)