如何覆盖flask-security的登录名? [英] how to override login of flask-security?

查看:100
本文介绍了如何覆盖flask-security的登录名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户登录时进行一些自定义.问题是该项目使用的是flask-security,它暗中处理用户登录. 我想在用户登录时检查数据库中用户的一些记录.

I want to do some customization when a user login. The problem is that the project is using flask-security which is implicitly handles user login. I want to check some records of users in the database when users login.

我如何在flask-security中覆盖登录"功能?

how can i override "login" function in flask-security?

我看到了类似的帖子,尝试过但不起作用. 另外,这也不是我想要做的. 如果某些用户,我可能需要停止默认行为.

I saw similar post, tried but not working. Plus, it is not exactly what I want to do. I maybe need to stop the default behavior in case of some users.

那么,有人遇到这种问题吗?我该怎么办?

So, anyone having this kind of issue? how can I do it?

谢谢!

推荐答案

设置Flask-Security时,您可以覆盖登录表单:

You can override the login form when you are setting up Flask-Security:

user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore, login_form=CustomLoginForm)

然后创建自定义登录表单类,以扩展默认的LoginForm.并覆盖validate函数,以便在尝试登录之前或之后进行操作.

Then create your custom login form class that extends the default LoginForm. And override the validate function to do stuff before or after the login attempt.

from flask_security.forms import LoginForm

class CustomLoginForm(LoginForm):
    def validate(self):
        # Put code here if you want to do stuff before login attempt

        response = super(CustomLoginForm, self).validate()

        # Put code here if you want to do stuff after login attempt

        return response

根据登录是否成功,响应"将为True或False.如果要检查尝试登录期间发生的可能错误,请参阅"self.errors"

"reponse" will be True or False based on if the login was successful or not. If you want to check possible errors that occurred during the login attempt see "self.errors"

问候, 凯文;)

这篇关于如何覆盖flask-security的登录名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆