Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask-login only works if get_id() returns self.email

my User class does not inherit from UserMixin but rather defines its own members and functions necessary for flask-login.

the login process only works if i set get_id() to return the instance email. if i attempt to do this on the user's name or actual id i don't get an error but i notice i am not logged (i am unable to access url's that require me to be logged in using @required_login)

class User(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    email = db.Column(db.Unicode(128))
    name = db.Column(db.Unicode(128))
    password = db.Column(db.Unicode(1024))
    authenticated = db.Column(db.Boolean, default=False)
    posts = db.relationship('Post')
    #-----login requirements-----
    def is_active(self):
        # all users are active
        return True 

    def get_id(self):
        # returns the user e-mail
        return unicode(self.email)

    def is_authenticated(self):
        return self.authenticated

    def is_anonymous(self):
        # False as we do not support annonymity
        return False

EDIT: at the time asking the question i did not understand get_id() needs to match load_user()

@login_manager.user_loader
def load_user(email):
    return User.get_user(email)
like image 737
Gil Hiram Avatar asked Jan 22 '16 16:01

Gil Hiram


1 Answers

it only works this way because get_id() needs to match login_manager.user_loader which in my case is expecting the email of the user.

like image 164
Gil Hiram Avatar answered Oct 12 '22 23:10

Gil Hiram