Skip to content

Commit 1550206

Browse files
author
azmanabdlh
committed
custom auth django [email, password] - (using django contrib auth)
1 parent 6893446 commit 1550206

File tree

11 files changed

+129
-4
lines changed

11 files changed

+129
-4
lines changed
311 Bytes
Binary file not shown.
1.9 KB
Binary file not shown.

account/admin.py

+10
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
from django.contrib import admin
2+
from django.contrib.auth.admin import UserAdmin
3+
4+
from .models import User
5+
6+
7+
class UserAdminConfig(UserAdmin):
8+
model = User
9+
10+
11+
admin.site.register(User, UserAdminConfig)

account/migrations/0001_initial.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Generated by Django 3.1.7 on 2021-03-05 08:00
2+
3+
import account.models
4+
import django.contrib.auth.validators
5+
from django.db import migrations, models
6+
import django.utils.timezone
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
initial = True
12+
13+
dependencies = [
14+
('auth', '0012_alter_user_first_name_max_length'),
15+
]
16+
17+
operations = [
18+
migrations.CreateModel(
19+
name='User',
20+
fields=[
21+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
22+
('password', models.CharField(max_length=128, verbose_name='password')),
23+
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
24+
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
25+
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
26+
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
27+
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
28+
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
29+
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
30+
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, null=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
31+
('email', models.EmailField(error_messages={'unique': 'A user with that email address already exists.'}, max_length=254, unique=True, verbose_name='email address')),
32+
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
33+
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
34+
],
35+
options={
36+
'verbose_name': 'user',
37+
'verbose_name_plural': 'users',
38+
'abstract': False,
39+
},
40+
managers=[
41+
('objects', account.models.AccountUserManager()),
42+
],
43+
),
44+
]

account/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.

account/models.py

+73-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
11
from django.db import models
2-
from django.contrib.auth.models import AbstractUser
3-
from django.utils.translation import gettext_lazy as _
2+
from django.contrib.auth.models import AbstractUser, BaseUserManager
3+
from django.contrib.auth.validators import UnicodeUsernameValidator
4+
from django.utils.translation import gettext_lazy as _
5+
6+
7+
class AccountUserManager(BaseUserManager):
8+
use_in_migrations = True
9+
10+
def _create_user(self, email, password, **kwargs):
11+
if not email:
12+
raise ValueError(_('You must provide an email address'))
13+
14+
email = self.normalize_email(email)
15+
user = self.model(email=email, **kwargs)
16+
user.set_password(password)
17+
user.save(self._db)
18+
19+
return user
20+
21+
def create_superuser(self, email, password, **kwargs):
22+
kwargs.setdefault('is_staff', True)
23+
kwargs.setdefault('is_superuser', True)
24+
kwargs.setdefault('is_active', True)
25+
26+
if kwargs.get('is_staff') is not True:
27+
raise ValueError('Superuser must be assigned to is_staff=True')
28+
29+
if kwargs.get('is_superuser') is not True:
30+
raise ValueError('Superuser must be assigned to is_superuser=True')
31+
32+
return self._create_user(email, password, **kwargs)
33+
34+
35+
36+
class User(AbstractUser):
37+
"""
38+
An abstract class to implementing a fully featured AbtractUser model
39+
with admin-compliant permissions.
40+
41+
Email and password are required. Other fields are optional.
42+
"""
43+
username_validator = UnicodeUsernameValidator()
44+
45+
# nullable username
46+
username = models.CharField(
47+
_('username'),
48+
max_length=150,
49+
unique=False,
50+
null=True,
51+
blank=False,
52+
help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
53+
validators=[username_validator],
54+
error_messages={
55+
'unique': _("A user with that username already exists."),
56+
},
57+
)
58+
59+
email = models.EmailField(
60+
_('email address'),
61+
unique=True,
62+
blank=False,
63+
error_messages = {
64+
'unique': _("A user with that email address already exists.")
65+
}
66+
)
67+
68+
# replaces Username with Email as the user model identifier .
69+
USERNAME_FIELD = 'email'
70+
71+
REQUIRED_FIELDS = []
72+
73+
# override
74+
objects = AccountUserManager()

account/templates/registration/register.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ <h1>Form Register page</h1>
1010
{{ form.as_ul }}
1111

1212
<button>Register</button>
13-
<a href="{% url 'register' %}">Login</a>
13+
<a href="{% url 'login' %}">Login</a>
1414
</form>
1515

1616
{% endblock %}
37 Bytes
Binary file not shown.

myapp/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134

135135
# Auth
136136

137-
# AUTH_USER_MODEL = 'account.User'
137+
AUTH_USER_MODEL = 'account.User'
138138

139139
LOGIN_REDIRECT_URL = '/home'
140140

0 commit comments

Comments
 (0)