2017-07-04 34 views
2

我想在用戶登錄時進行一些自定義。問題是該項目正在使用flask-security,它隱式處理用戶登錄。 我想在用戶登錄時檢查數據庫中的用戶記錄。如何覆蓋flask-security的登錄?

我如何重寫flask安全中的「登錄」功能?

我看到similar post,試過但不工作。另外,這不完全是我想要做的。 我可能需要在某些用戶的情況下停止默認行爲。

那麼,有這種問題的人呢?我該怎麼做?

謝謝!

回答

3

您可以覆蓋登錄表單,當你設置的燒瓶安全:

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

然後創建一個擴展默認LoginForm的自定義登錄表單類。並重寫驗證函數在登錄嘗試之前或之後執行一些操作。

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 

根據登錄是否成功,「reponse」將爲True或False。如果您想檢查登錄嘗試期間可能發生的錯誤,請參閱「self.errors」

問候, Kevin;)