diff --git a/config/authh.py b/config/authh.py new file mode 100644 index 0000000..186366c --- /dev/null +++ b/config/authh.py @@ -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 \ No newline at end of file diff --git a/config/settings.py b/config/settings.py index 5a4e9c3..91c2dba 100644 --- a/config/settings.py +++ b/config/settings.py @@ -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", diff --git a/templates/great.html b/templates/great.html new file mode 100644 index 0000000..379d6e6 --- /dev/null +++ b/templates/great.html @@ -0,0 +1,7 @@ +{% extends "authBase.html" %} + +{% block title %}Log Out{% endblock %} + +{% block content %} +

zalogowano

+{% endblock %} \ No newline at end of file diff --git a/templates/login.html b/templates/login.html index a8442a1..fcc6db3 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,16 +1,120 @@ -{% extends "authBase.html" %} + -{% block title %}Log In{% endblock %} + + Login + + + -{% block content %} -

Log In

-
-

Email

- -

Password

- - -

Remember Me

-
-{% endblock %} + + + + +
+
+
+
+ + +

LOGIN

+
+
+
+ {% csrf_token %} +
+
+ +
+ +
+
+
+ +
+ +
+ + +
+ +
+ +
+ + +
+
+
+
+ + + \ No newline at end of file diff --git a/trials/views.py b/trials/views.py index 12dcd13..fd086a5 100644 --- a/trials/views.py +++ b/trials/views.py @@ -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() diff --git a/users/urls.py b/users/urls.py index 1c75f05..f910099 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 +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'), diff --git a/users/views.py b/users/views.py index b62d505..cd8f607 100644 --- a/users/views.py +++ b/users/views.py @@ -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)