Merge pull request 'Added login' (#20) from feature/authentication-in-browser into master

Reviewed-on: #20
This commit is contained in:
s470631 2021-12-19 17:03:06 +01:00
commit ba37eeb6f4
7 changed files with 176 additions and 24 deletions

21
config/authh.py Normal file
View File

@ -0,0 +1,21 @@
from django.conf import settings
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.hashers import check_password
from users.models import User
class SettingsBackend(BaseBackend):
def authenticate(self, request, email=None, password=None):
user = User.objects.get(email=email)
if check_password(password, user.password):
user_final = user
else:
user_final = ({"message": "bad"})
return user_final
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

View File

@ -48,6 +48,7 @@ APPEND_SLASH = False
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
"django.contrib.gis",
"rest_framework",
@ -60,10 +61,12 @@ INSTALLED_APPS = [
"questions",
"categories"
]
# AUTHENTICATION_BACKENDS = ['config.authh.SettingsBackend']
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.security.SecurityMiddleware',
"corsheaders.middleware.CorsMiddleware",
'django.contrib.auth.middleware.AuthenticationMiddleware',
# "`debug_toolbar.middleware.DebugToolbarMiddleware`",
'django.middleware.common.CommonMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
@ -113,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",

7
templates/great.html Normal file
View File

@ -0,0 +1,7 @@
{% extends "authBase.html" %}
{% block title %}Log Out{% endblock %}
{% block content %}
<p>zalogowano</p>
{% endblock %}

View File

@ -1,16 +1,120 @@
{% extends "authBase.html" %}
<html>
{% block title %}Log In{% endblock %}
<head>
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
{% block content %}
<h2>Log In</h2>
<form action="">
<p>Email</p>
<input type="email" placeholder="Enter Email">
<p>Password</p>
<input type="password" placeholder="Enter Password">
<input type="submit" value="Log in">
<p><input type="checkbox">Remember Me</p>
</form>
{% endblock %}
<style>
body,
html {
margin: 0;
padding: 0;
height: 100%;
background: #7abecc !important;
}
.user_card {
width: 350px;
margin-top: auto;
margin-bottom: auto;
background: #74cfbf;
position: relative;
display: flex;
justify-content: center;
flex-direction: column;
padding: 10px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-webkit-box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-moz-box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
border-radius: 5px;
}
.form_container {
margin-top: 20px;
}
#form-title{
color: #fff;
}
.login_btn {
width: 100%;
background: #33ccff !important;
color: white !important;
}
.login_btn:focus {
box-shadow: none !important;
outline: 0px !important;
}
.login_container {
padding: 0 2rem;
}
.input-group-text {
background: #f7ba5b !important;
color: white !important;
border: 0 !important;
border-radius: 0.25rem 0 0 0.25rem !important;
}
.input_user,
.input_pass:focus {
box-shadow: none !important;
outline: 0px !important;
}
#messages{
background-color: grey;
color: #fff;
padding: 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container h-100">
<div class="d-flex justify-content-center h-100">
<div class="user_card">
<div class="d-flex justify-content-center">
<h3 id="form-title">LOGIN</h3>
</div>
<div class="d-flex justify-content-center form_container">
<form method="POST" action="">
{% csrf_token %}
<div class="input-group mb-3">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input type="text" name="username" placeholder="Username..." class="form-control">
</div>
<div class="input-group mb-2">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
<input type="password" name="password" placeholder="Password..." class="form-control" >
</div>
<div class="d-flex justify-content-center mt-3 login_container">
<input class="btn login_btn" type="submit" value="Login">
</div>
</form>
</div>
<div class="mt-4">
<div class="d-flex justify-content-center links">
Don't have an account? <a href="{% url 'register' %}" class="ml-2">Sign Up</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

View File

@ -3,7 +3,6 @@ from rest_framework import views
from rest_framework import viewsets
from rest_framework.response import Response
from config.settings import BASE_DIR
from trials.models import Test
from trials.serializers import TestSerializer
@ -15,9 +14,6 @@ class TestModelViewSet(viewsets.ModelViewSet):
class TestTemplateView(TemplateView):
permission_classes = []
template_name = BASE_DIR + f"/templates/generic_test.html"
def get_queryset(self):
return Test.objects.all()

View File

@ -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
from .views import PasswordReset, UserPasswordResetConfirmView, RegisterViewSet, login, logout, register, register_success, loginn, login_success
router = DefaultRouter(trailing_slash=False)
@ -13,9 +13,10 @@ router.register("items", UserModelViewSet)
urlpatterns = [
path("", include(router.urls)),
path('login', login, name='login'),
path('login', loginn, name='login'),
path('logout', logout, name='logout'),
path('register/success', register_success, name='register_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'),

View File

@ -1,3 +1,4 @@
from django.http import HttpResponseRedirect
from rest_framework import viewsets
from users.models import User
@ -10,7 +11,10 @@ from rest_framework.response import Response
from django.shortcuts import render, redirect
from django.template import loader
from .forms import RegistrationForm
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login as auth_login
from config.authh import SettingsBackend
from django.contrib.auth.decorators import login_required
class UserModelViewSet(viewsets.ModelViewSet):
@ -99,3 +103,19 @@ def register(request):
form = RegistrationForm()
context['registration_form'] = form
return render(request, 'register.html', context)
def login_success(request):
return render(request, 'great.html')
def loginn(request):
if request.POST:
username = request.POST.get("username")
password = request.POST.get("password")
user = SettingsBackend().authenticate(request, email=username, password=password)
if user is not None:
auth_login(request, user)
context = {}
return render(request, 'login.html', context)