Search code examples
flaskflask-security

How to prevent flask-security to auto login after registration?


When a user is registered, I do not want the user to log in automatically, instead the page should redirect to the login page. Here are my configs:

app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_CONFIRMABLE'] = False
app.config['SECURITY_SEND_REGISTER_EMAIL'] = False
app.config['USER_ENABLE_CONFIRM_EMAIL'] = False
app.config['SECURITY_POST_LOGIN_VIEW'] = '/'
app.config['SECURITY_POST_REGISTER_VIEW'] = '/login_redirect/'

Below is the /login_redirect/ route defination

@app.route('/login_redirect/')
@login_required
def login_redirect():
    return redirect(url_for('security.login'))

Solution

  • Call logout_user() and then do your redirect.

    from flask_security.utils import logout_user
    
    @app.route('/login_redirect/')
    @login_required
    def login_redirect():
        logout_user()
        return redirect(url_for('security.login'))