jeudi 26 juillet 2018

User Login with flask-login and peewee

I am trying to use flask-login to log in users. I am using peewee to store the users with the following model:

class User(Model):
    id = PrimaryKeyField()
    pin = IntegerField()
    is_authenticated = BooleanField(default=False)
    is_active = BooleanField(default=True)
    is_anonymous = BooleanField(default=True)

For testing purpose I am using the id as login name and the pin as password. My login function in flask looks like follows:

def login():
    if request.method == 'POST':
        request_json =request.get_json()
        user_id= request_json["user_id"]
        pin = request_json["pin"]
        user = User.get(id=user_id)

        if int(pin) == user.pin:
            login_user(user):
            return jsonify({'loggedIn': True})
        else:
            return abort(401)

Unfortunately, the is_authenticated value in the database is never set. I suspect, that flask-login is setting it to 1 but is never saving the object and therefore it never gets set.

The docs of flask-login do not have a very detailed description how it is done.

login_user() does return true, if a user is logged in and could therefore be used as a constrain to set the users is_authenticated value to 1. Is there any nicer way to do so?




Aucun commentaire:

Enregistrer un commentaire