lingthio / Flask-User

Customizable User Authorization & User Management: Register, Confirm, Login, Change username/password, Forgot password and more.
http://flask-user.readthedocs.io/
MIT License
1.06k stars 292 forks source link

How to change flash messages. #324

Open feerbau opened 4 years ago

feerbau commented 4 years ago

Hello i would like to know if it's possible to change default flask-user flash messages. For example the ones in "user_manager__views.py" When i use @login_required it redirects the user to the login page and shows this message "You must be signed in to access {here_the_url}."

What can i do if i want to show another message? Is it possible?

Chaostheorie commented 3 years ago

Use a custom user manager. You will be able to change the function used for the view below is an example that should be enough to give you an idea on where to start. Haven't tested it but should be able to do the job.

from flask_user.user_manager import UserManager
from flask import redirect, flash

class CustomUserManager(UserManager):
    def unauthenticated_view(self):
        """ Prepare a Flash message and redirect to USER_UNAUTHENTICATED_ENDPOINT"""
        # Prepare Flash message
        url = request.url
        flash(_("You must be signed in to access '%(url)s'.", url=url), 'error') # Insert your own message

        # Redirect to USER_UNAUTHENTICATED_ENDPOINT
        safe_next_url = self.make_safe_url(url)
        return redirect(self._endpoint_url(self.USER_UNAUTHENTICATED_ENDPOINT)+'?next='+quote(safe_next_url))

user_manager = CustomUserManager(app, db, User)