I am using Flask-Login and trying to understand how it works. If I understand the docs right, I should create a user class which holds four methods, is_authenticated, is_active, is_anonymous and get_id. I don't really understand what I should do after that. There is a function, login_user(user), which I understand would take my instance of a not yet authenticated user and authenticate it, but how does that work when my user class does not have any set methods?
Here is some code:
class User:
isAuth = False
isActive = False
isAnon = True
id = unicode(0)
def __init__(self, isAuth, isActive, isAnon, id):
self.isAuth = isAuth
self.isActive = isActive
self.isAnon = isAnon
self.id = id
def is_authenticated(self):
return isAuth
def is_active(self):
return isActive
def is_anonymous(self):
return isAnon
def get_id(self):
return id
And here is my guess about how I should authenticate a user, by using authomatic and Flask-Login:
@app.route('/login/<provider_name>/', methods=['GET', 'POST'])
def login(provider_name):
response = make_response()
result = authomatic.login(WerkzeugAdapter(request, response), provider_name)
user = User()
if result:
if result.user:
result.user.update()
login_user(user)
return render_template('login.html', result=result)
return response
What am I missing?
EDIT, here is the user_loader:
@lm.user_loader
def load_user(userid):
return User.get(userid)
And I also have this on top:
lm = LoginManager()
lm.init_app(app)
@login_required
decorator above your methods that handle responses (likeindex()
or similar). – Bo MilanovichisAuth
->is_auth
, or evenIS_AUTH
if that supposed to be constants – akaRem