Skip to content

Commit 6ba1916

Browse files
Merge pull request #112 from Aerstone/master
Add support for keeping the user logged in after password change (Django 1.7+)
2 parents 680f24e + 48eb40a commit 6ba1916

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

docs/api_endpoints.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ Basic
3030
- new_password1
3131
- new_password2
3232
- old_password
33-
34-
33+
34+
3535
.. note:: ``OLD_PASSWORD_FIELD_ENABLED = True`` to use old_password.
36+
.. note:: ``LOGOUT_ON_PASSWORD_CHANGE = False`` to keep the user logged in after password change
3637

3738
- /rest-auth/user/ (GET)
3839

docs/configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ Configuration
3434

3535

3636
- **OLD_PASSWORD_FIELD_ENABLED** - set it to True if you want to have old password verification on password change enpoint (default: False)
37+
38+
- **LOGOUT_ON_PASSWORD_CHANGE** - set to False if you want to keep the current user logged in after a password change

rest_auth/serializers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from rest_framework import serializers, exceptions
1313
from rest_framework.authtoken.models import Token
1414
from rest_framework.exceptions import ValidationError
15+
from django.contrib.auth import update_session_auth_hash
1516

1617

1718
class LoginSerializer(serializers.Serializer):
@@ -182,6 +183,9 @@ def __init__(self, *args, **kwargs):
182183
self.old_password_field_enabled = getattr(
183184
settings, 'OLD_PASSWORD_FIELD_ENABLED', False
184185
)
186+
self.logout_on_password_change = getattr(
187+
settings, 'LOGOUT_ON_PASSWORD_CHANGE', False
188+
)
185189
super(PasswordChangeSerializer, self).__init__(*args, **kwargs)
186190

187191
if not self.old_password_field_enabled:
@@ -212,3 +216,5 @@ def validate(self, attrs):
212216

213217
def save(self):
214218
self.set_password_form.save()
219+
if not self.logout_on_password_change:
220+
update_session_auth_hash(self.request, self.user)

0 commit comments

Comments
 (0)