Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to login vis username or email #94 #96

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exclude_patterns:
- "**/migrations/*"
- "**/tests.py"
- "dev.py"
- "*ideal*"

plugins:
bandit:
Expand Down
28 changes: 28 additions & 0 deletions accounts/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from django.db.models import Q


UserModel = get_user_model()


class EmailBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = UserModel.objects.get(
Q(username__iexact=username) | Q(email__iexact=username),
)
except UserModel.DoesNotExist:
UserModel().set_password(password)
return
except UserModel.MultipleObjectsReturned:
user = (
UserModel.objects.filter(
Q(username__iexact=username) | Q(email__iexact=username),
)
.order_by("id")
.first()
)

if user.check_password(password) and self.user_can_authenticate(user):
return user
12 changes: 10 additions & 2 deletions accounts/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from django.contrib.auth.forms import (
UserCreationForm,
UserChangeForm,
AuthenticationForm,
)
from django import forms
from .models import User


Expand All @@ -13,3 +17,7 @@ class CustomUserChangeForm(UserChangeForm):
class Meta:
model = User
fields = ("username", "email")


class LoginForm(AuthenticationForm):
username = forms.CharField(label="Email / Username")
18 changes: 18 additions & 0 deletions accounts/migrations/0003_alter_user_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0 on 2024-01-15 22:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0002_alter_user_table'),
]

operations = [
migrations.AlterField(
model_name='user',
name='email',
field=models.EmailField(max_length=254, unique=True, verbose_name='email address'),
),
]
3 changes: 3 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.db import models
from django.db.models import QuerySet
from django.contrib.auth.models import AbstractUser
from django.utils.translation import gettext_lazy as _


class User(AbstractUser):
email = models.EmailField(_("email address"), unique=True)

class Meta:
db_table = "user"
verbose_name = _("user")
Expand Down
3 changes: 2 additions & 1 deletion accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path
from django.utils.translation import gettext_lazy as _
from .views import SignUpView
from .views import LoginView, SignUpView

urlpatterns = [
path(_("signup/"), SignUpView.as_view(), name="signup"),
path("login/", LoginView.as_view(), name="login"),
]
9 changes: 7 additions & 2 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView

from .forms import CustomUserCreationForm
from django.contrib.auth import views as auth_views
from .forms import LoginForm, CustomUserCreationForm


class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy("home")
template_name = "registration/signup.html"


class LoginView(auth_views.LoginView):
form_class = LoginForm
template_name = "registration/login.html"
2 changes: 1 addition & 1 deletion core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@

LOGIN_REDIRECT_URL = "home"
LOGOUT_REDIRECT_URL = "home"

AUTHENTICATION_BACKENDS = ["accounts.backends.EmailBackend"] # new

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
Expand Down