Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Logout Callback Functionality #39

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,27 @@ url_for('simplelogin.login', next='http://myothersite.com/')
You can use the `from werkzeug.security import check_password_hash, generate_password_hash` utilities to encrypt passwords.

A working example is available in `manage.py` of [example app](https://github.com/flask-extensions/Flask-SimpleLogin/tree/main/example)

## Registering Custom Logout Callback(s)

You can define multiple custom logout callbacks to be executed after the user logs out using the `register_on_logout_callback` method:

```python
from flask import Flask
from flask_simplelogin import SimpleLogin

app = Flask(__name__)
app.config['SECRET_KEY'] = 'something-secret'
simple_login = SimpleLogin(app)

def my_post_logout_callback():
print("User has logged out")

def another_post_logout_callback():
print("Another action after logout")

simple_login.register_on_logout_callback(my_post_logout_callback)
simple_login.register_on_logout_callback(another_post_logout_callback)
```

The callbacks will be executed in the order they were registered.
9 changes: 9 additions & 0 deletions flask_simplelogin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def __init__(self, app=None, login_checker=None, login_form=None, messages=None)
self.app = None
self._login_checker = login_checker or default_login_checker
self._login_form = login_form or LoginForm
self.on_logout_callbacks = []
if app is not None:
self.init_app(
app=app,
Expand Down Expand Up @@ -348,7 +349,15 @@ def login(self):

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

def register_on_logout_callback(self, callback):
"""Register a callback to be called on logout"""
self.on_logout_callbacks.append(callback)

def logout(self):
session.clear()
self.flash("logout")

for callback in self.on_logout_callbacks:
callback()

return redirect(self.config.get("home_url", "/"))
12 changes: 11 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from base64 import b64encode
from unittest.mock import call
from unittest.mock import call, Mock

from flask import url_for

Expand Down Expand Up @@ -80,6 +80,13 @@ def test_is_logged_in_from_json_request(app, client):


def test_logout(app, client, csrf_token_for):
callback1 = Mock()
callback2 = Mock()

simplelogin = app.extensions["simplelogin"]
simplelogin.register_on_logout_callback(callback1)
simplelogin.register_on_logout_callback(callback2)

client.get(url_for("simplelogin.login"))
assert not is_logged_in()
client.post(
Expand All @@ -94,6 +101,9 @@ def test_logout(app, client, csrf_token_for):
client.get(url_for("simplelogin.logout"))
assert not is_logged_in()

callback1.assert_called_once()
callback2.assert_called_once()


def test_flash(app, mocker):
mock_flash = mocker.patch("flask_simplelogin.flash")
Expand Down
Loading