Auth login message when not logged in - patch

45 views
Skip to first unread message

Tom Clerckx

unread,
Jun 11, 2025, 11:40:00 AM6/11/25
to py4web
Please find below a suggestion to customize or skip the login flash message when the user is not logged-in.

Patch:

diff --git a/py4web/utils/auth.py b/py4web/utils/auth.py
index ad328447..624631f3 100644
--- a/py4web/utils/auth.py
+++ b/py4web/utils/auth.py
@@ -80,7 +80,8 @@ class AuthEnforcer(Fixture):
redirect_next = request.fullpath
if request.query_string:
redirect_next = redirect_next + "?{}".format(request.query_string)
- self.auth.flash.set(message)
+ if str(message):
+ self.auth.flash.set(message)
redirect(
URL(
self.auth.route,
@@ -120,7 +121,7 @@ class AuthEnforcer(Fixture):
self.auth.on_request(context)
if "user" not in self.auth.session or "id" not in self.auth.session["user"]:
- self.goto_login(message="Login required")
+ self.goto_login(message=self.auth.param.messages["flash"].get("login-required"))
if callable(self.condition) and not self.condition(user):
self.abort_or_redirect("not-authorized", "User not authorized")
@@ -151,6 +152,7 @@ class Auth(Fixture):
"user-logout": "User logout",
"email-verified": "Email verified",
"link-expired": "Link invalid or expired",
+ "login-required": "Login required,"
},
"labels": {
"username": "Username",


To set the message:
auth.param.messages["flash"]["login-required"] = T("---THY MUS LOG IN--")

To skip the entire flash message:
auth.param.messages["flash"]["login-required"] = ""

Tom Clerckx

unread,
Jun 12, 2025, 10:22:47 AM6/12/25
to py4web
I saw that you checked in the patch....
One additional change below.
1)   Same condition in the DefaultAuthForms class
2)   It seems that sometimes the empty message is converted to a string with only a Byte-order-marker ("\ufeff"). 
    I don't know where this comes from, but if the message only contains this, the flash message should not be set either.

diff --git a/py4web/utils/auth.py b/py4web/utils/auth.py
index 3ca0218a..a7d750e8 100644
--- a/py4web/utils/auth.py
+++ b/py4web/utils/auth.py
@@ -80,7 +80,7 @@ class AuthEnforcer(Fixture):
redirect_next = request.fullpath
if request.query_string:
redirect_next = redirect_next + "?{}".format(request.query_string)
- if message and str(message):
+ if message and str(message) and str(message) != "\ufeff":
self.auth.flash.set(message)
redirect(
URL(
@@ -2067,7 +2067,9 @@ class DefaultAuthForms:
self._postprocessing("verify_email")
def _set_flash(self, key):
- self.auth.flash.set(self.auth.param.messages["flash"].get(key, key))
+ message = self.auth.param.messages["flash"].get(key, key)
+ if message and str(message) and str(message) != "\ufeff":
+ self.auth.flash.set(message)
def _postprocessing(self, action, form=None, user=None):
if action in self.auth.on_accept:


Reply all
Reply to author
Forward
0 new messages