Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily Override Login Message from Specific Redirected Route with Flask-Login

I have a Flask app and am using Flask-Login to manage user authentication. The main route ('/') requires login, however I would not like it to flash the error message defined for this specific route. I would like it to behave as it should flashing the error messages for other routes but not this one. Here is a snippet from my init.py

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view =  'login'
login_manager.login_message = 'You Must Login to Access This Page!'
login_manager.login_message_category = 'error'

The following is from my views.py

@app.route('/')
@login_required
def dashboard():
    return render_template('dashboard.html')

@app.route('/login', methods=["GET", "POST"])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = Employee.query.filter_by(email=form.email.data).first()
        if user and user.is_valid_pass(form.password.data):
            remember = form.remember.data == 'y'
            login_user(user, remember=remember)
            next = request.args.get('next')
            return redirect(next or url_for('dashboard'))
        else:
            flash(u'Incorrect Username or Password!', 'error')
            return redirect(url_for('login'))
    return render_template('login.html', form=form)

As you can see I have the main route password protected, and because of the login_manager settings it is flashing the message "You Must Login to Access This Page!" and of course I'm displaying those errors on the login template. However, how can I ensure that this functions accordingly for every route EXCEPT the main ('/') route? I've been through the documentation but I see nothing that would indicate it could be done.

Can I simply overwrite the login_manager.login_message with None in that specific route? If so, how would the login manager be appropriately called, overwritten, and then changed back?

like image 363
ThatTechGuy Avatar asked Dec 09 '15 17:12

ThatTechGuy


People also ask

How to redirect a URL in flask?

The Flask class has a redirect () function. When invoked, it returns a response object and redirects the user to another target location with the specified status code. Sometimes you need to redirect an URL, like when the url is no longer available or the user is not logged in.

How to create a multi-user login system in flask?

If you want a multi-user login system, you should add a database layer to the application. Flask does not have out of the box database support. You have to use a third party library if you want database support. In this tutorial we will use SqlAlchemy. If you do not have that installed type:

How do I exit a code loop in flask?

Using abort function to exit from the code loop, in conjunction to redirect. Here the code refers to a set of codes that will generate some error, as applicable, and stop any further execution of the application. How does redirect Function Works in Flask?

What do you like most about flask?

Some noteworthy features include securing parts of our app behind login walls, encrypting passwords, and handling sessions. Moreover, It plays nicely with other Flask libraries we’re already familiar with: Flask-SQLAlchemy to create and fetch accounts, and Flask-WTForms for handling intelligent sign-up & log-in forms.


1 Answers

What I ended up doing was simply passing the value of the GET variable "next" that Flask-Login uses in these cases to the template from the view. Then choosing not to display flashes in the case where it matches the route I don't want to display from.

In the view:

next = request.args.get('next')
render_template('dashboard.html' form=form, next=next)

In the template:

{% if next != '/' %}
  //Loop Displaying Flash Errors
{% endif %}

There may be an easier way to discard flashes in the view and you still want to make sure you have something doing get_flashed_messages() otherwise they'll just pile up at some point when you actually do display flash.

like image 115
ThatTechGuy Avatar answered Nov 03 '22 14:11

ThatTechGuy