From 0f52b54a2ecee908da1968098a6766b99cf3c256 Mon Sep 17 00:00:00 2001 From: Piotr Kopycki Date: Mon, 20 Dec 2021 19:40:24 +0100 Subject: [PATCH] Authentication frontend update --- config/settings.py | 9 +++++--- config/urls.py | 5 +++-- config/views.py | 10 +++++++-- static/style.css | 24 ++++++++++++++++++++ templates/{index.html => home.html} | 0 templates/login2.html | 21 ++++++++++++++++++ templates/welcome.html | 11 ++++++++++ users/forms.py | 13 +++++++++-- users/urls.py | 6 ++--- users/views.py | 34 +++++++++++++++++++++-------- 10 files changed, 112 insertions(+), 21 deletions(-) rename templates/{index.html => home.html} (100%) create mode 100644 templates/login2.html create mode 100644 templates/welcome.html diff --git a/config/settings.py b/config/settings.py index 91c2dba..8a9df16 100644 --- a/config/settings.py +++ b/config/settings.py @@ -116,9 +116,9 @@ REST_FRAMEWORK = { "DEFAULT_PERMISSION_CLASSES": ( "rest_framework.permissions.IsAuthenticated", ), -# "DEFAULT_AUTHENTICATION_CLASSES": ( -# "rest_framework_simplejwt.authentication.JWTAuthentication", -# ), + # "DEFAULT_AUTHENTICATION_CLASSES": ( + # "rest_framework_simplejwt.authentication.JWTAuthentication", + # ), "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination", "DEFAULT_FILTER_BACKENDS": ( "django_filters.rest_framework.DjangoFilterBackend", @@ -207,3 +207,6 @@ CORS_ALLOW_HEADERS = ('content-disposition', 'accept-encoding', 'content-type', 'accept', 'origin', 'authorization') MEDIA_URL = "/media/" + +LOGIN_URL='welcome' +LOGIN_REDIRECT_URL='welcome' diff --git a/config/urls.py b/config/urls.py index a6e2117..8dbd9b2 100644 --- a/config/urls.py +++ b/config/urls.py @@ -16,10 +16,11 @@ Including another URLconf from django.contrib import admin from django.urls import include from django.urls import path -from .views import index +from .views import home, welcome urlpatterns = [ - path('', index), + path('', welcome, name='welcome'), + path('home', home, name='home'), path('users/', include("users.urls")), path('questions/', include("questions.urls")), path('answers/', include("answers.urls")), diff --git a/config/views.py b/config/views.py index 7aa2528..9623ff2 100644 --- a/config/views.py +++ b/config/views.py @@ -1,9 +1,15 @@ from django.shortcuts import render, redirect from django.template import loader +from django.contrib.auth.decorators import login_required -def index(request): +@login_required +def home(request): # context = { # 'latest_question_list': latest_question_list, # } - return render(request, 'index.html') + return render(request, 'home.html') + + +def welcome(request): + return render(request, 'welcome.html') diff --git a/static/style.css b/static/style.css index 056942f..442b19c 100644 --- a/static/style.css +++ b/static/style.css @@ -23,6 +23,10 @@ color: #00916E; } +.sidenav-footer { + +} + .main { margin-left: 160px; padding: 0px 10px; @@ -51,6 +55,12 @@ text-transform: uppercase; } +.authContent h3 { + margin: 0; + padding: 0 0 20px; + text-align: center; +} + .authContent p { margin: 0; padding: 0; @@ -106,4 +116,18 @@ input[type=checkbox]{ 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 diff --git a/templates/index.html b/templates/home.html similarity index 100% rename from templates/index.html rename to templates/home.html diff --git a/templates/login2.html b/templates/login2.html new file mode 100644 index 0000000..60ab399 --- /dev/null +++ b/templates/login2.html @@ -0,0 +1,21 @@ +{% extends "authBase.html" %} + +{% block title %}Login{% endblock %} + +{% block content %} +

Log In

+
{% csrf_token %} + + {% for field in login_form %} +

+ {{field.label_tag}} + {{field}} + {% for error in field.errors %} +

{{error}}

+ {% endfor %} +

+ {% endfor %} + +
+{% endblock %} + diff --git a/templates/welcome.html b/templates/welcome.html new file mode 100644 index 0000000..6657948 --- /dev/null +++ b/templates/welcome.html @@ -0,0 +1,11 @@ +{% extends "authBase.html" %} + +{% block title %}Super Original and Innovative Test App{% endblock %} + +{% block content %} +

Super Original and Innovative Test App

+

Get started now

+ + +{% endblock %} + diff --git a/users/forms.py b/users/forms.py index fdefaaa..87838e5 100644 --- a/users/forms.py +++ b/users/forms.py @@ -1,5 +1,5 @@ from django import forms -from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UsernameField from .models import User class RegistrationForm(UserCreationForm): @@ -11,4 +11,13 @@ class RegistrationForm(UserCreationForm): class Meta: model = User - fields = ("email", "first_name", "last_name", "password1", "password2") \ No newline at end of file + 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") diff --git a/users/urls.py b/users/urls.py index f910099..aa89add 100644 --- a/users/urls.py +++ b/users/urls.py @@ -4,7 +4,7 @@ from django.urls import path from users.views import UserModelViewSet from rest_framework_simplejwt.views import TokenObtainPairView 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) @@ -13,10 +13,10 @@ router.register("items", UserModelViewSet) urlpatterns = [ path("", include(router.urls)), - path('login', loginn, name='login'), + path('login', login, name='login'), path('logout', logout, name='logout'), 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', register, name='register'), path('api/token', TokenObtainPairView.as_view(), name='token_obtain_pair'), diff --git a/users/views.py b/users/views.py index cd8f607..c4b4fb8 100644 --- a/users/views.py +++ b/users/views.py @@ -10,7 +10,7 @@ from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSe from rest_framework.response import Response from django.shortcuts import render, redirect 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 import login as auth_login from config.authh import SettingsBackend @@ -70,12 +70,6 @@ class UserPasswordResetConfirmView(PasswordResetConfirmShortcut, generics.Generi token_generator = default_token_generator -def login(request): - # context = { - # 'latest_question_list': latest_question_list, - # } - return render(request, 'login.html') - def logout(request): # context = { # 'latest_question_list': latest_question_list, @@ -109,7 +103,29 @@ def login_success(request): 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: username = request.POST.get("username") password = request.POST.get("password") @@ -117,5 +133,5 @@ def loginn(request): user = SettingsBackend().authenticate(request, email=username, password=password) if user is not None: auth_login(request, user) - context = {} + return redirect('home') return render(request, 'login.html', context)