0

I refer to the information in Flask-Login github

Part of it is written like this, def user_loader(email), I see

user = User()
user.id = email

But in User() I can't find any about id and how to use this function on that example

Thanks for your reply!

pcymichael
  • 95
  • 7

1 Answers1

1

Look it's very simple, in the 'example' they show there isn't. Is in fact a blank template.

By reporting what they have, in the same page you posted check for this:

class User(flask_login.UserMixin):
    pass

As you can see the user flask (with flask.login.UserMixin inherited) is empty, is filled with a pass. Normally you may fill it up as follow:

EXAMPLE

class User(flask_login.UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(30), unique=True)
    password = db.Column(db.String(30))
    session_token = db.Column(db.String(100), unique=True)

Here do a CTRL + F and search for (class User(UserMixin):), here is what follows:

class User(UserMixin):
    def __init__(self, name, id, active=True):
        self.id = id
        self.name = name
        self.active = active

What does UserMixIn?

Here You can see how they implemented the UserMixin in which User() will inherit:

class UserMixin(object):
    '''
    This provides default implementations for the methods that Flask-Login
    expects user objects to have.
    '''

    if not PY2:  # pragma: no cover
        # Python 3 implicitly set __hash__ to None if we override __eq__
        # We set it back to its default implementation
        __hash__ = object.__hash__

    @property
    def is_active(self):
        return True

    @property
    def is_authenticated(self):
        return True

    @property
    def is_anonymous(self):
        return False

    def get_id(self):
        try:
            return text_type(self.id)
        except AttributeError:
            raise NotImplementedError('No `id` attribute - override `get_id`')

    def __eq__(self, other):
        '''
        Checks the equality of two `UserMixin` objects using `get_id`.
        '''
        if isinstance(other, UserMixin):
            return self.get_id() == other.get_id()
        return NotImplemented

    def __ne__(self, other):
        '''
        Checks the inequality of two `UserMixin` objects using `get_id`.
        '''
        equal = self.__eq__(other)
        if equal is NotImplemented:
            return NotImplemented
        return not equal
  • is_authenticated: a property that is True if the user has valid credentials or False otherwise.

  • is_active: a property that is True if the user's account is active or False otherwise.

  • is_anonymous: a property that is False for regular users, and True for a special, anonymous user.

  • get_id(): a method that returns a unique identifier for the user as a string (unicode, if using Python 2).

Usefull links:

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
  • Thanks, it helps me a lot but it feels complicated – pcymichael Aug 20 '20 at 13:59
  • It is ineed, not easy, look at this course https://www.youtube.com/watch?v=2dEM-s3mRLE&list=PLXmMXHVSvS-CoYS177-UvMAQYRfL3fBtX&index=84 is pretty printed, I also add it to the answer. this will help you a lot, I think go the basic first then go deep into their github repository – Federico Baù Aug 20 '20 at 15:40