Skip to content

Commit 0d663bf

Browse files
authored
Merge pull request #39 from Zingzy/logout-callback
Add Logout Callback Functionality
2 parents 2d4b7b0 + 2f17574 commit 0d663bf

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

docs/configuring.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,27 @@ url_for('simplelogin.login', next='http://myothersite.com/')
9090
You can use the `from werkzeug.security import check_password_hash, generate_password_hash` utilities to encrypt passwords.
9191

9292
A working example is available in `manage.py` of [example app](https://github.com/flask-extensions/Flask-SimpleLogin/tree/main/example)
93+
94+
## Registering Custom Logout Callback(s)
95+
96+
You can define multiple custom logout callbacks to be executed after the user logs out using the `register_on_logout_callback` method:
97+
98+
```python
99+
from flask import Flask
100+
from flask_simplelogin import SimpleLogin
101+
102+
app = Flask(__name__)
103+
app.config['SECRET_KEY'] = 'something-secret'
104+
simple_login = SimpleLogin(app)
105+
106+
def my_post_logout_callback():
107+
print("User has logged out")
108+
109+
def another_post_logout_callback():
110+
print("Another action after logout")
111+
112+
simple_login.register_on_logout_callback(my_post_logout_callback)
113+
simple_login.register_on_logout_callback(another_post_logout_callback)
114+
```
115+
116+
The callbacks will be executed in the order they were registered.

flask_simplelogin/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def __init__(self, app=None, login_checker=None, login_form=None, messages=None)
196196
self.app = None
197197
self._login_checker = login_checker or default_login_checker
198198
self._login_form = login_form or LoginForm
199+
self.on_logout_callbacks = []
199200
if app is not None:
200201
self.init_app(
201202
app=app,
@@ -348,7 +349,15 @@ def login(self):
348349

349350
return render_template("login.html", form=form, next=destiny), ret_code
350351

352+
def register_on_logout_callback(self, callback):
353+
"""Register a callback to be called on logout"""
354+
self.on_logout_callbacks.append(callback)
355+
351356
def logout(self):
352357
session.clear()
353358
self.flash("logout")
359+
360+
for callback in self.on_logout_callbacks:
361+
callback()
362+
354363
return redirect(self.config.get("home_url", "/"))

tests/test_app.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from base64 import b64encode
2-
from unittest.mock import call
2+
from unittest.mock import call, Mock
33

44
from flask import url_for
55

@@ -80,6 +80,13 @@ def test_is_logged_in_from_json_request(app, client):
8080

8181

8282
def test_logout(app, client, csrf_token_for):
83+
callback1 = Mock()
84+
callback2 = Mock()
85+
86+
simplelogin = app.extensions["simplelogin"]
87+
simplelogin.register_on_logout_callback(callback1)
88+
simplelogin.register_on_logout_callback(callback2)
89+
8390
client.get(url_for("simplelogin.login"))
8491
assert not is_logged_in()
8592
client.post(
@@ -94,6 +101,9 @@ def test_logout(app, client, csrf_token_for):
94101
client.get(url_for("simplelogin.logout"))
95102
assert not is_logged_in()
96103

104+
callback1.assert_called_once()
105+
callback2.assert_called_once()
106+
97107

98108
def test_flash(app, mocker):
99109
mock_flash = mocker.patch("flask_simplelogin.flash")

0 commit comments

Comments
 (0)