- October 19, 2007
- middleware auth logout
- 5 (after 5 ratings)
This Middleware is to log users out after a certain amount of time has passed. You'll want to add AUTO_LOGOUT_DELAY to your settings.py, set to a number of minutes after which a user should be logged out.
It adds the key 'last_touch' to the session, you'll want to change that if you happen to be using that already.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from django.conf import settings from django.contrib import auth from datetime import datetime, timedelta class AutoLogout: def process_request(self, request): if not request.user.is_authenticated() : #Can't log out if not logged in return try: if datetime.now() - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0): auth.logout(request) del request.session['last_touch'] return except KeyError: pass request.session['last_touch'] = datetime.now()
More like this
- Decorator to logout user based on a test and/or redirect to another url by vemubalu 5 years, 4 months ago
- StrictAuthentication - Auto log-out inactive users by yeago 7 years, 7 months ago
- Show logged users - keeping track of users login and logout by albertorcf 3 years, 7 months ago
- Login + logout script from a nub by alandelevie 7 years, 4 months ago
- Log to syslog by fylb 6 years, 2 months ago