I'm trying to add another column in my User class for an API key. However, I get an error with my kwargs when using Flask-Security.
class User(db.Model, UserMixin):
__tablename__ = 'usermod'
id = db.Column(db.Integer(), primary_key=True)
email = db.Column(db.String(120), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
api_key = db.Column(db.String(256))
roles = db.relationship('Role', secondary=roles_users, backref=db.backref(
'users', lazy='dynamic'))
Furthermore, I'm trying to use my own generate_key()
function to fill the field for the api_key
for each user after registration. This is given that I am using Flask-Security's built-in app.config['SECURITY_REGISTERABLE'] = True
field. I'm not really sure how to override Flask-Security's settings to do this.
Okay, I solved this by simply keeping the User class as above. But to add my own function I had to use signals like so:
@signals.user_confirmed.connect_via(app)
def user_confirmed_sighandler(app, user):
user.api_key = generate_key()
return
This occurs after the user confirms their registration in email as opposed to at registration time (in which case you would use @signals.user_registered.connect_via(app)
.