how to run custom code at login with flask-security

  • Last Update :
  • Techknowledgy :

I think using a signal decorator seems to be the easiest-- in the following example on_user_logged_in should be called when a user logs into your app. More info in the docs.

from flask.ext.login
import user_logged_in

def on_user_logged_in(sender, user):
   log_auth_token(user.get_auth_token()) # or whatever.

Suggestion : 2

Flask-Security bootstraps your application with various views for handling its configured features to get you up and running as quickly as possible. However, you’ll probably want to change the way these views look to be more in line with your application’s visual design.,Flask-Security will likely be a very small piece of your application, so Flask-Security makes it easy to override all aspects of API responses.,Calls render_template (as configured at Flask-Security initialization time) with the context and template to produce a text and/or html version of the message,For a very long read and discussion; look at this. Out of the box, Flask-Security in tandem with Flask-Login, behave as follows:

security = Security(app, user_datastore)

# This processor is added to all templates
def security_context_processor():
   return dict(hello = "world")

# This processor is added to only the register view
def security_register_processor():
   return dict(something = "else")
from flask_security
import RegisterForm
from wtforms
import StringField
from wtforms.validators
import DataRequired

class ExtendedRegisterForm(RegisterForm):
   first_name = StringField('First Name', [DataRequired()])
last_name = StringField('Last Name', [DataRequired()])

security = Security(app, user_datastore,
   register_form = ExtendedRegisterForm)
class User(db.Model, UserMixin):
   id = db.Column(db.Integer, primary_key = True)
email = db.Column(db.String(255), unique = True)
password = db.Column(db.String(255))
first_name = db.Column(db.String(255))
last_name = db.Column(db.String(255))
import flask_babel

app.config["SECURITY_MSG_INVALID_PASSWORD"] = ("Password no-worky", "error")

Suggestion : 3

Answer link :,I started out by trying to override this function by importing flask.ext.login (which normally, I would not need to do) and redefining the function as follows:,I am new to flask, but moderately proficient in python - I have a flask app that uses flask-security for user authentication. I would like to add some additional functionality to the user login process. Specifically, I need to save the user's auth_token (which I have set up to be a one-time-use token) to the db when they login, and remove it when they log out. The issue comes because flask-security does not (to my knowledge) expose the machinery of logging in directly to the developer. As far as I can tell from the code, it imports flask-login, which uses a login_user function.,I was thinking there might be a way to do it with a decorator, but I am new to flask, and have not used decorators much regardless.

I started out by trying to override this function by importing flask.ext.login (which normally, I would not need to do) and redefining the function as follows:

import flask.ext.login as adef new\ _login\ _user(): ...copy of existing
function goes here......Plus new stuff with current_user.get_auth_token()...a.login_user = new_login_user