update login with email

This commit is contained in:
Sokurov Idar
2021-03-02 21:44:13 +03:00
parent a6c8910b63
commit f50ce49d77
6 changed files with 68 additions and 18 deletions

19
access_controller/auth.py Normal file
View File

@@ -0,0 +1,19 @@
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class EmailAuthBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
return None
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

View File

@@ -135,6 +135,12 @@ ACCOUNT_ACTIVATION_DAYS = 7
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
# Название_приложения.Названиеайла.Название_класса_обработчика
AUTHENTICATION_BACKENDS = [
'access_controller.auth.EmailAuthBackend',
]
# Logging system
# https://docs.djangoproject.com/en/3.1/topics/logging/
LOGGING = {

View File

@@ -14,17 +14,18 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.views import LoginView
from django.contrib.auth import views as auth_views
from django.urls import path, include
from main.views import main_page, profile_page, CustomRegistrationView
from main.views import main_page, profile_page, CustomRegistrationView, CustomLoginView
urlpatterns = [
path('admin/', admin.site.urls, name='admin'),
path('', main_page, name='index'),
path('accounts/profile/', profile_page, name='profile'),
path('accounts/register/', CustomRegistrationView.as_view(), name='registration'),
path('accounts/login/', LoginView.as_view(extra_context={}), name='login'), # TODO add extra context
path('accounts/login/', CustomLoginView.as_view(extra_context={}), name='login',), # TODO add extra context
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/', include('django_registration.backends.activation.urls')),
path('accounts/login/', include('django.contrib.auth.urls')),