How do you track the current user in flask-login?
Asked Answered
P

4

28

I'm trying to use the current user in my view from flask-login. So I tried to use the g object

I'm assigning flask.ext.login.current_user to the g object

@pot.before_request
def load_users():
    g.user = current_user.username

It works if the user is correct. But when I sign-up or login with the wrong credentials I get this error:

`AttributeError: 'AnonymousUserMixin' object has no attribute 'username'`

Please enlight me where am I wrong...

Plafker answered 9/10, 2013 at 13:59 Comment(0)
P
20

Thanks for your answer @Joe and @pjnola, as you all suggested i referred flask-login docs

I found that we can customize the anonymous user class, so i customized for my requirement,

Anonymous class

#!/usr/bin/python
#flask-login anonymous user class
from flask.ext.login import AnonymousUserMixin
class Anonymous(AnonymousUserMixin):
  def __init__(self):
    self.username = 'Guest'

Then added this class to anonymous_user

login_manager.anonymous_user = Anonymous

From this it was able to fetch the username if it was anonymous request.

Plafker answered 9/10, 2013 at 14:41 Comment(3)
I would recommend having your Anonymous object inherit from AnonymousUserMixin, rather than reimplementing it all over again. That way, if Flask-Login changes its API, you get those changes. Also, get_id should be a method that returns None, not "None" itself.Karissakarita
I would go with what Mark says on this: class Anonymous(AnonymousUserMixin):Tsai
Sure @MarkHildreth, yes that is the better way of implementing, ThanksPlafker
D
12

Well, the error message says it all. There is no logged in user, so current_user returns an AnonymousUserMixin. AnonymousUserMixin implements the interface described here: http://flask-login.readthedocs.org/en/latest/#your-user-class (which does not include a username property). Try something like this:

@pot.before_request
def load_users():
    if current_user.is_authenticated():
        g.user = current_user.get_id() # return username in get_id()
    else:
        g.user = None # or 'some fake value', whatever

Obviously, the rest of your code has to deal with the possibility that g.user will not refer to a real user.

Delinda answered 9/10, 2013 at 14:25 Comment(0)
T
6

AnonymousUserMixin has no username attribute. You need to overwrite the object and call the mixin. Have a look at LoginManager.anonymous_user which is an object which is used when no user is logged in.

You also need to get the user from somewhere. There is no point in storing the username in g as you could just use current_user.username.

If you wanted to get the username you would need too

if current_user.is_authenticated():
    g.user = current_user.username

This would require that the user object has a property called username. There are lots of ways to customize Flask-Logins use

I suggest re-reading the docs and taking a look at the source code:

https://github.com/maxcountryman/flask-login/blob/master/flask_login.py

Joe

Tsai answered 9/10, 2013 at 14:15 Comment(2)
In latest version of flask login, 'is_authenticated' is converted into a bool variable. So the correct version is if current_user.is_authenticated : g_user = current_user.get_id()Peabody
Please update answer with correct is_authenticatedModulation
B
0

For finding if there is a loggin session we can do the following: (I have gone through all solutions and noted that when I use the below statements it is failing.)

if current_user.is_authenticated():
    g.user = current_user.username

The correct way is:

if current_user.is_authenticated:
    g.user = current_user.username

And then it works fine.

Blackness answered 24/11, 2022 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.