0
# Account Management
1
2
Local user authentication, registration, email verification, password management, and account settings. Handles traditional Django authentication flows with enhanced email and phone verification capabilities.
3
4
## Capabilities
5
6
### Models
7
8
Core models for user account management including email addresses, login tracking, and email confirmations.
9
10
```python { .api }
11
class EmailAddress(models.Model):
12
"""
13
Model for managing user email addresses with verification status.
14
"""
15
user: User = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
16
email: str = models.EmailField(max_length=app_settings.EMAIL_MAX_LENGTH)
17
verified: bool = models.BooleanField(default=False)
18
primary: bool = models.BooleanField(default=False)
19
20
objects: EmailAddressManager
21
22
def send_confirmation(self, request: HttpRequest, signup: bool = False) -> EmailConfirmation: ...
23
24
class EmailConfirmation(models.Model):
25
"""
26
Model for email confirmation tokens and verification workflow.
27
"""
28
email_address: EmailAddress
29
created: datetime
30
sent: Optional[datetime]
31
key: str
32
33
objects: EmailConfirmationManager
34
35
def confirm(self, request: HttpRequest) -> EmailAddress: ...
36
def send(self, request: HttpRequest, signup: bool = False) -> None: ...
37
38
class Login(models.Model):
39
"""
40
Model for tracking user login sessions and device information.
41
"""
42
user: User
43
ip_address: str
44
user_agent: str
45
timestamp: datetime
46
method: str
47
```
48
49
### Views
50
51
Django class-based views for authentication flows including login, signup, password management, and email verification.
52
53
```python { .api }
54
class LoginView(RedirectAuthenticatedUserMixin, AjaxCapableProcessFormViewMixin, FormView):
55
"""
56
User login view with form handling and redirection.
57
"""
58
form_class: type = LoginForm
59
template_name: str = "account/login.html"
60
61
def form_valid(self, form: LoginForm) -> HttpResponse: ...
62
63
class SignupView(RedirectAuthenticatedUserMixin, CloseableSignupMixin, AjaxCapableProcessFormViewMixin, FormView):
64
"""
65
User registration view with form handling and email verification.
66
"""
67
form_class: type = SignupForm
68
template_name: str = "account/signup.html"
69
70
def form_valid(self, form: SignupForm) -> HttpResponse: ...
71
72
class LogoutView(LogoutFunctionalityMixin, TemplateView):
73
"""
74
User logout view with session cleanup.
75
"""
76
template_name: str = "account/logout.html"
77
78
class PasswordResetView(AjaxCapableProcessFormViewMixin, FormView):
79
"""
80
Password reset request view with email sending.
81
"""
82
form_class: type = ResetPasswordForm
83
template_name: str = "account/password_reset.html"
84
85
class PasswordResetFromKeyView(AjaxCapableProcessFormViewMixin, FormView):
86
"""
87
Password reset confirmation view using token from email.
88
"""
89
form_class: type = ResetPasswordKeyForm
90
template_name: str = "account/password_reset_from_key.html"
91
92
class PasswordChangeView(AjaxCapableProcessFormViewMixin, FormView):
93
"""
94
Password change view for authenticated users.
95
"""
96
form_class: type = ChangePasswordForm
97
template_name: str = "account/password_change.html"
98
99
class EmailView(AjaxCapableProcessFormViewMixin, FormView):
100
"""
101
Email management view for adding/removing email addresses.
102
"""
103
form_class: type = AddEmailForm
104
template_name: str = "account/email.html"
105
106
class ConfirmEmailView(TemplateView):
107
"""
108
Email confirmation view using token from email.
109
"""
110
template_name: str = "account/email_confirm.html"
111
112
def get(self, request: HttpRequest, key: str) -> HttpResponse: ...
113
def post(self, request: HttpRequest, key: str) -> HttpResponse: ...
114
```
115
116
### Forms
117
118
Django forms for authentication flows with validation and custom field handling.
119
120
```python { .api }
121
class LoginForm(forms.Form):
122
"""
123
User login form with email/username and password fields.
124
"""
125
login: str = forms.CharField()
126
password: str = PasswordField()
127
remember: bool = forms.BooleanField(required=False)
128
129
def clean(self) -> Dict[str, Any]: ...
130
def login(self, request: HttpRequest, redirect_url: str = None) -> User: ...
131
132
class SignupForm(forms.Form):
133
"""
134
User registration form with username, email, and password fields.
135
"""
136
username: str = forms.CharField()
137
email: str = EmailField()
138
password1: str = SetPasswordField()
139
password2: str = PasswordField()
140
141
def clean_username(self) -> str: ...
142
def clean_email(self) -> str: ...
143
def clean_password2(self) -> str: ...
144
def save(self, request: HttpRequest) -> User: ...
145
146
class ResetPasswordForm(forms.Form):
147
"""
148
Password reset request form with email field.
149
"""
150
email: str = EmailField()
151
152
def clean_email(self) -> str: ...
153
def save(self, request: HttpRequest) -> None: ...
154
155
class ResetPasswordKeyForm(forms.Form):
156
"""
157
Password reset confirmation form with new password fields.
158
"""
159
password1: str = SetPasswordField()
160
password2: str = PasswordField()
161
162
def __init__(self, user: User, temp_key: str, *args, **kwargs): ...
163
def clean_password2(self) -> str: ...
164
def save(self) -> User: ...
165
166
class ChangePasswordForm(forms.Form):
167
"""
168
Password change form for authenticated users.
169
"""
170
oldpassword: str = PasswordField()
171
password1: str = SetPasswordField()
172
password2: str = PasswordField()
173
174
def __init__(self, user: User, *args, **kwargs): ...
175
def clean_oldpassword(self) -> str: ...
176
def clean_password2(self) -> str: ...
177
def save(self) -> User: ...
178
179
class AddEmailForm(forms.Form):
180
"""
181
Form for adding new email addresses to user account.
182
"""
183
email: str = EmailField()
184
185
def __init__(self, user: User, *args, **kwargs): ...
186
def clean_email(self) -> str: ...
187
def save(self, request: HttpRequest) -> EmailAddress: ...
188
```
189
190
### Adapters
191
192
Customizable adapter classes for extending authentication behavior.
193
194
```python { .api }
195
class AccountAdapter:
196
"""
197
Adapter class for customizing account-related behavior.
198
"""
199
200
def new_user(self, request: HttpRequest) -> User:
201
"""Create new user instance."""
202
203
def save_user(self, request: HttpRequest, user: User, form: forms.Form, commit: bool = True) -> User:
204
"""Save user instance with form data."""
205
206
def clean_username(self, username: str, shallow: bool = False) -> str:
207
"""Clean and validate username."""
208
209
def clean_email(self, email: str) -> str:
210
"""Clean and validate email address."""
211
212
def clean_password(self, password: str, user: User = None) -> str:
213
"""Clean and validate password."""
214
215
def send_mail(self, template_prefix: str, email: str, context: Dict[str, Any]) -> None:
216
"""Send email using template."""
217
218
def get_login_redirect_url(self, request: HttpRequest) -> str:
219
"""Get URL to redirect after login."""
220
221
def get_logout_redirect_url(self, request: HttpRequest) -> str:
222
"""Get URL to redirect after logout."""
223
224
def is_open_for_signup(self, request: HttpRequest) -> bool:
225
"""Check if signup is open."""
226
227
def get_adapter(request: HttpRequest = None) -> AccountAdapter:
228
"""
229
Get account adapter instance.
230
231
Parameters:
232
- request: Optional HTTP request object
233
234
Returns:
235
AccountAdapter instance
236
"""
237
```
238
239
### Utilities
240
241
Account-related utility functions for user management and email handling.
242
243
```python { .api }
244
def filter_users_by_email(email: str, is_active: bool = None, prefetch: bool = False) -> QuerySet:
245
"""
246
Filter users by email address.
247
248
Parameters:
249
- email: Email address to filter by
250
- is_active: Optional filter by user active status
251
- prefetch: Whether to prefetch related data
252
253
Returns:
254
User queryset
255
"""
256
257
def filter_users_by_username(*usernames: str) -> QuerySet:
258
"""
259
Filter users by username(s).
260
261
Parameters:
262
- usernames: Username(s) to filter by
263
264
Returns:
265
User queryset
266
"""
267
268
def setup_user_email(request: HttpRequest, user: User, addresses: List[str]) -> None:
269
"""
270
Set up user email addresses.
271
272
Parameters:
273
- request: HTTP request object
274
- user: User instance
275
- addresses: List of email addresses to add
276
"""
277
278
def user_email(user: User) -> str:
279
"""
280
Get primary email address for user.
281
282
Parameters:
283
- user: User instance
284
285
Returns:
286
Primary email address or empty string
287
"""
288
289
def user_username(user: User) -> str:
290
"""
291
Get username for user.
292
293
Parameters:
294
- user: User instance
295
296
Returns:
297
Username string
298
"""
299
300
def send_email_confirmation(request: HttpRequest, user: User, signup: bool = False) -> EmailConfirmation:
301
"""
302
Send email confirmation to user.
303
304
Parameters:
305
- request: HTTP request object
306
- user: User instance
307
- signup: Whether this is during signup flow
308
309
Returns:
310
EmailConfirmation instance
311
"""
312
```
313
314
## Usage Examples
315
316
### Basic User Registration
317
318
```python
319
from allauth.account.forms import SignupForm
320
from allauth.account.models import EmailAddress
321
322
# Process signup form
323
form_data = {
324
'username': 'johndoe',
325
'email': 'john@example.com',
326
'password1': 'secure_password123',
327
'password2': 'secure_password123'
328
}
329
330
form = SignupForm(form_data)
331
if form.is_valid():
332
user = form.save(request)
333
print(f"User {user.username} created")
334
```
335
336
### Email Management
337
338
```python
339
from allauth.account.models import EmailAddress
340
341
# Add email address
342
email_addr = EmailAddress.objects.add_email(request, user, 'new@example.com')
343
344
# Send confirmation
345
confirmation = email_addr.send_confirmation(request)
346
347
# Check verification status
348
if email_addr.verified:
349
print("Email is verified")
350
351
# Set as primary
352
email_addr.set_as_primary()
353
```
354
355
### Password Reset Flow
356
357
```python
358
from allauth.account.forms import ResetPasswordForm, ResetPasswordKeyForm
359
360
# Request password reset
361
reset_form = ResetPasswordForm({'email': 'user@example.com'})
362
if reset_form.is_valid():
363
reset_form.save(request)
364
print("Password reset email sent")
365
366
# Handle reset confirmation (from email link)
367
key_form = ResetPasswordKeyForm(
368
user=user,
369
temp_key=temp_key,
370
data={
371
'password1': 'new_password123',
372
'password2': 'new_password123'
373
}
374
)
375
if key_form.is_valid():
376
user = key_form.save()
377
print("Password reset successfully")
378
```
379
380
### Custom Adapter
381
382
```python
383
from allauth.account.adapter import DefaultAccountAdapter
384
385
class MyAccountAdapter(DefaultAccountAdapter):
386
def get_login_redirect_url(self, request):
387
return '/dashboard/'
388
389
def is_open_for_signup(self, request):
390
# Custom signup logic
391
return request.user.is_staff
392
393
def send_mail(self, template_prefix, email, context):
394
# Custom email sending logic
395
super().send_mail(template_prefix, email, context)
396
397
# In settings.py
398
ACCOUNT_ADAPTER = 'myapp.adapters.MyAccountAdapter'
399
```