Skip to content

Commit 072a0a7

Browse files
committed
README: case-insensitive usernames
1 parent d508b94 commit 072a0a7

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,28 @@ The registration is disabled by default. To enable it, the following value is re
117117
REGISTRATION_ENABLED = True
118118
```
119119

120+
## Case-Insensitive Usernames
121+
122+
To make usernames case-insensitive (and thus, prevent the registration of multiple identical usernames with different cases), you could overwrite the `get_by_natural_key` to search for existing usernames case-insensitively:
123+
124+
```python
125+
class CustomUserManager(UserManager):
126+
def get_by_natural_key(self, username):
127+
username_attr = '{}__iexact'.format(self.model.USERNAME_FIELD)
128+
return self.get(**{username_attr: username})
129+
130+
class User(AbstractUser):
131+
...
132+
133+
objects = CustomUserManager()
134+
135+
...
136+
```
137+
138+
`django-rest-authtoken` uses the `get_by_natural_key` method upon registration to verify the uniqueness of the username.
120139

121140
### Confirmation Email
141+
122142
It is possible to optionally enable an email confirmation. An email will be sent upon registration to the provided email address. For this to work, the user model needs to contain a `BooleanField` that stores whether the email address has been confirmed already. If email confirmation should be mandatory to be able to login, this can be set to the `active` field of the user (which is respected by django-rest-authtoken upon login).
123143

124144
A minimal example of a compatible user model could look like this:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='django-rest-authtoken',
8-
version='2.1.0',
8+
version='2.1.1',
99
author='Pascal Wichmann',
1010
author_email='[email protected]',
1111
description='A simple token-based auth backend for Django Rest Framework storing cryptographically hashed tokens on server-side.',

0 commit comments

Comments
 (0)