gpt4 book ai didi

python - Flask-Login 检查用户是否在没有装饰器的情况下通过身份验证

转载 作者:IT老高 更新时间:2023-10-28 21:57:59 24 4
gpt4 key购买 nike

从 Flask-Login 文档中,它描述了系统用户如何要求经过身份验证的用户模型来访问使用装饰器语法的方法:

from flask_login import login_required    

@app.route("/settings")
@login_required
def settings():
pass

现在一切都很好,但我希望能够检查用户是否在某个方法中进行了身份验证,如下所示:

@app.route('/main/', methods=['GET', 'POST'])
main_route():
if request.method == 'GET':
if user_is_authenticated(): #Do the authentication here
#load authenticated /main/ html template etc.
pass
else:
#load unauthenticated /main/ html template etc.
pass
...

这样做的原因是因为它分解了 GET 和 POST 请求,而不是为经过身份验证的用户和未经身份验证的用户复制路由。
我怎样才能做到这一点?有可能吗?

最佳答案

这在 flask 中很简单:

from flask_login import current_user

@app.route(...)
def main_route():
if current_user.is_authenticated:
return render_template("main_for_user.html")
else:
return render_template("main_for_anonymous.html")

the documentation on anonymous users .

关于python - Flask-Login 检查用户是否在没有装饰器的情况下通过身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20419228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com