User Sign-out question

I know this is pretty basic, but how do you implement signout functionality with the Users service built into anvil?

I’m not really sure why I can’t find this in the docs…

Hi @nathanguyette,

you can find it here:

anvil.users.logout()

https://anvil.works/docs/users/presenting-a-login-form

2 Likes

Humm… I guess I should be more specific.

When logging out, I need it to force a login screen again.

How do I do this?

My apps are arranged so that the default form checks to see if I’m logged in, and then redirects me to either a login page or a content page accordingly.

So, after calling anvil.users.logout(), I can just take the user back to that default form with open_form() .

1 Like

Thank you, but can you give me an example of how this works?

This is off the top of my head so might require some tweaking.

In your landing page (startup form) show event :

if anvil.users.get_user():
   open_form("my_private_form")
else:
   open_form("my_login_form")

And in your “my_private_form” form, have a button that does this when clicked :

def logout_button_click(self, **event_args):
    anvil.users.logout()
    open_form("landing_page")

You could just open the “my_login_form” directly, but I tend to do a little more than just a simple logged in/not logged in so I have the extra step in there. Others may advise differently, and you can adjust to suit.

Note also, that the “landing page” can now be a start up module and doesn’t have to be a form. The docs will help you there.

edit - ah, yes. As @stucork’s link below says, you’ll need to use a form rather than a start up module to do it my way as you cannot open_form() on a module.

2 Likes

I typically do something like this post - where the behaviour of logging out is the same behaviour that I would use in the startup module

Very similar to @david.wylie’s snippet above.

1 Like