samedi 29 février 2020

How to implement Guest User in Flask?

I'm trying to create a guest user in my flask app, but have found it to be a pain. I need a different user experience when they choose to be a 'guest' - it'll be triggered by a 'continue with guest' button click. Here's what I've done so far:

class AnonymousUser(AnonymousUserMixin):
    def __init__(self):
        super()
        self.is_guest=False

class GuestUser(AnonymousUserMixin):
    def __init__(self):
        super()
        self.is_guest=True

So initially, I set

loginmanager.anonymous_user = AnonymousUser

and when the 'continue as guest' button is pressed, I set

loginmanager.anonymous_user = GuestUser

I've noticed that this successfully changed the anon user in the login manager, but it doesn't change the current_user provided by flask-login. Printing flask-login after setting the anon user in the login manager to GuestUser still gives

<app.models.AnonymousUser object at 0x111422222>

Can anyone highlight the flaw in this approach or suggest a different approach? Thanks!




Aucun commentaire:

Enregistrer un commentaire