简体   繁体   中英

Flask-Security @login_required decorator customize redirect

How would I override flask-security @login_required redirect URL?

Currently when an user tried to access a page with @login_required decorater; they're prompted to login page by default, however I want to be able to redirect user to /register page instead; since both login/register forms are combined together under one URL/Template. Is it possible to do so?

I've tried this so far and doesn't seem to be working.

SECURITY_UNAUTHORIZED_REDIRECT = '/register'

Update: This works for flask_login package.

You can do this using LoginManager.login_view

In my app, I have used this like below:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

from app import views,models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM