Skip to content

Commit

Permalink
Redirect user to the login page after session timeout instead of the …
Browse files Browse the repository at this point in the history
…root page
  • Loading branch information
tleguijt committed Dec 11, 2017
1 parent 20fa593 commit 6259fcd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.0.3 (xxxx-xx-xx)
==================
- Redirect user to the login page after session timeout instead of the root page


0.0.2 (2017-11-10)
==================
- Added setting to control if session should be expired X seconds after last
Expand Down
4 changes: 2 additions & 2 deletions src/django_session_timeout/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time

from django.conf import settings
from django.shortcuts import redirect
from django.contrib.auth.views import redirect_to_login

try:
from django.utils.deprecation import MiddlewareMixin
Expand All @@ -26,7 +26,7 @@ def process_request(self, request):

if session_is_expired:
request.session.flush()
return redirect('/')
return redirect_to_login(next=request.path)

expire_since_last_activity = getattr(
settings, 'SESSION_EXPIRE_AFTER_LAST_ACTIVITY', False)
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
def pytest_configure():
settings.configure(
INSTALLED_APPS=[
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions'
],
MIDDLEWARE_CLASSES=[],
Expand Down
6 changes: 3 additions & 3 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_session_expire(r, settings):
with freeze_time('2017-08-31 22:46:01'):
response = middleware.process_request(r)
assert SESSION_TIMEOUT_KEY not in r.session
assert response['location'] == '/'
assert response['location'] == '/accounts/login/?next=/'


def test_session_expire_no_expire_setting(r, settings):
Expand All @@ -60,7 +60,7 @@ def test_session_expire_no_expire_setting(r, settings):
with freeze_time('2017-08-31 22:46:01'):
response = middleware.process_request(r)
assert SESSION_TIMEOUT_KEY not in r.session
assert response['location'] == '/'
assert response['location'] == '/accounts/login/?next=/'


def test_session_expire_last_activity(r, settings):
Expand All @@ -80,4 +80,4 @@ def test_session_expire_last_activity(r, settings):
with freeze_time('2017-08-31 23:46:02'):
response = middleware.process_request(r)
assert SESSION_TIMEOUT_KEY not in r.session
assert response['location'] == '/'
assert response['location'] == '/accounts/login/?next=/'

0 comments on commit 6259fcd

Please sign in to comment.