A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/atugushev/django-password-session/ below:

atugushev/django-password-session: A reusable Django app that invalidates all active sessions after change password

A reusable Django app that will invalidate all active sessions after change password.

Since Django 1.7 this feature implemented out of the box.

  1. Install a package.
$ pip install django-password-session
  1. Add "password_session" to your INSTALLED_APPS setting:
INSTALLED_APPS = (
    ...
    'password_session',
)
  1. Add middleware:
MIDDLEWARE_CLASSES = (
    ...
    'password_session.middleware.CheckPasswordHash',
),
  1. Make sure that you have the following settings:
INSTALLED_APPS = (
    ...
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
)

AUTHENTICATION_BACKENDS = (
    ...
    'django.contrib.auth.backends.ModelBackend',
)

MIDDLEWARE_CLASSES = (
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
  1. To avoid logging out a user from a current session you should update the session by calling the following function directly after change a password:
from password_session import update_session_auth_hash
update_session_auth_hash(request, user)

It's a very simple view for change password just for demonstrating how to update a current session.

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

from password_session import update_session_auth_hash


def change_password_view(request):
    user = request.user
    user.set_password(request.POST.get('password'))
    user.save()
    update_session_auth_hash(request, user)
    return HttpResponse("Hello, %s! Your password has been changed!" % user.username)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4