Skip to content

Commit 8f0ffd3

Browse files
authored
make post redirects default to APPLICATION_ROOT config (#376)
1 parent c1d8426 commit 8f0ffd3

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

docs/configuration.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,16 @@ Login/Logout
533533
.. py:data:: SECURITY_POST_LOGIN_VIEW
534534
535535
Specifies the default view to redirect to after a user logs in. This value can be set to a URL
536-
or an endpoint name.
536+
or an endpoint name. Defaults to the Flask config ``APPLICATION_ROOT`` value which itself defaults to ``"/"``.
537537

538-
Default: ``"/"``.
538+
Default: ``APPLICATION_ROOT``.
539539

540540
.. py:data:: SECURITY_POST_LOGOUT_VIEW
541541
542-
Specifies the default view to redirect to after a user logs out.
543-
This value can be set to a URL or an endpoint name.
542+
Specifies the default view to redirect to after a user logs out. This value can be set to a URL
543+
or an endpoint name. Defaults to the Flask config ``APPLICATION_ROOT`` value which itself defaults to ``"/"``.
544544

545-
Default: ``"/"``.
545+
Default: ``APPLICATION_ROOT``.
546546

547547

548548
.. py:data:: SECURITY_UNAUTHORIZED_VIEW

flask_security/core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,14 @@ def init_app(self, app, datastore=None, register_blueprint=None, **kwargs):
10311031
if "mail_util_cls" not in kwargs:
10321032
kwargs.setdefault("mail_util_cls", MailUtil)
10331033

1034+
# default post redirects to APPLICATION_ROOT, which itself defaults to "/"
1035+
app.config.setdefault(
1036+
"SECURITY_POST_LOGIN_VIEW", app.config.get("APPLICATION_ROOT", "/")
1037+
)
1038+
app.config.setdefault(
1039+
"SECURITY_POST_LOGOUT_VIEW", app.config.get("APPLICATION_ROOT", "/")
1040+
)
1041+
10341042
for key, value in _default_config.items():
10351043
app.config.setdefault("SECURITY_" + key, value)
10361044

flask_security/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def find_redirect(key):
525525
rv = (
526526
get_url(session.pop(key.lower(), None))
527527
or get_url(current_app.config[key.upper()] or None)
528-
or "/"
528+
or current_app.config.get("APPLICATION_ROOT", "/")
529529
)
530530
return rv
531531

tests/test_misc.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,3 +942,41 @@ def myview():
942942
# This should work and not be redirected
943943
response = client.get("/myview", follow_redirects=False)
944944
assert response.status_code == 200
945+
946+
947+
def test_post_security_with_application_root(app, sqlalchemy_datastore):
948+
init_app_with_options(app, sqlalchemy_datastore, **{"APPLICATION_ROOT": "/root"})
949+
client = app.test_client()
950+
951+
response = client.post(
952+
"/login", data=dict(email="matt@lp.com", password="password")
953+
)
954+
assert response.status_code == 302
955+
assert response.headers["Location"] == "http://localhost/root"
956+
957+
response = client.get("/logout")
958+
assert response.status_code == 302
959+
assert response.headers["Location"] == "http://localhost/root"
960+
961+
962+
def test_post_security_with_application_root_and_views(app, sqlalchemy_datastore):
963+
init_app_with_options(
964+
app,
965+
sqlalchemy_datastore,
966+
**{
967+
"APPLICATION_ROOT": "/root",
968+
"SECURITY_POST_LOGIN_VIEW": "/post_login",
969+
"SECURITY_POST_LOGOUT_VIEW": "/post_logout",
970+
}
971+
)
972+
client = app.test_client()
973+
974+
response = client.post(
975+
"/login", data=dict(email="matt@lp.com", password="password")
976+
)
977+
assert response.status_code == 302
978+
assert response.headers["Location"] == "http://localhost/post_login"
979+
980+
response = client.get("/logout")
981+
assert response.status_code == 302
982+
assert response.headers["Location"] == "http://localhost/post_logout"

0 commit comments

Comments
 (0)