Announcing Persistent Login and Cookies

Hi folks,

It’s been another busy week here at Anvil, and I’m delighted to announce some new features that we launched today. There are two big ones amongst the usual batch of tweaks and improvements:

Cookies

Now you can store a small amount of persistent data in the user’s browser using Anvil Cookies. There are two cookies - one that is private to your app, and one that is shared between all your (or your organisation’s) apps. See the full documentation for anvil.server.cookies here. We have worked hard behind the scenes to make this usable and secure, and it has allowed us to launch our most-requested feature:

…drum roll…

Persistent Login

The Users Service now includes a “Remember me” option. This is fully configurable in the Users Service Config:

image

It’s disabled by default, so your existing apps will work just as before until you choose to switch it on. When enabled, users will see a “Remember me” check box on the login form, and anvil.users.get_user() will remember users between sessions, so you can decide whether to display the login form or not. It’s that simple. And just like everything else in Anvil, it’s fully customisable from code - all the anvil.users.* functions take an optional keyword argument remember, so you can use this for custom signup/login forms too.

Now for the pièce de résistance:

image

If you have multiple apps that share the same “Users” table, user login status can be remembered between apps. This option simply chooses between using the local or shared Anvil Cookie for saving state, so it’s also completely customisable should the need arise.

Check out the updated documentation for the Users Service here, or head to the config page for the Users Service in your existing app and try it out!


As always, we welcome feedback and suggestions, so do let us know what you think below.

4 Likes

Marvellous - thank you chaps. That is most useful.

On a related note, LastPass password manager doesn’t pick up the email field in the login form to offer a stored username, but it does pick up the password field. Unfortunately it only fills in the password field as well.

I don’t now what the criteria for LastPass is (yet), but I’m guessing the email text box needs some form of identifier on it.

Any chance you could look that at some point please? I know, never satisfied :slight_smile:

Yes, it’s annoying isn’t it? LastPass seems to have absolutely no consistency when it comes to identifying login forms, so we’ve struggled to make this work. I will give it another go!

I just altered the CSS directly in the browser for the input text box. I added :

name="username"

and it worked!

Could you maybe just do that?

Yes, that would work (it seems), but as always we have to make sure this works cleanly when building a custom form too. Will look into it!

1 Like

What about accessing cookies for the domain that were set by another non-anvil application?

For example, I have a custom subdomain setup for my Anvil app. Another application (Django) on this domain has set a cookie named “anvil-test”. However, it isn’t available through either anvil.server.cookies.local/shared. How can I access this cookie from my anvil app?

Yes this is awesome! Thanks for setting this up!

However:

I’m getting some weird behavior when I turn on ‘Remember Me’ (or use my own cookie to store login status). Here’s the code for my startup form _login; it either directs the user on to the main screen _play if they are logged in, or lets them log in.

class _login (_loginTemplate):
  def __init__(self, **properties):
    self.init_components(**properties)

    me = anvil.users.get_user(allow_remembered=True)

    if me:
      print('already logged in')
      open_form('_play')
      # some_code_that_raises_error     # this is the strange part

    else:
      print('not logged in')
      self.content_panel.add_component(Login())
  • If there is no stored information (or if I delete it), the login screen comes up and everything works.

  • If there is a stored login, everything looks normal in the console, but the _play page never becomes visible - I get an empty layout with my menu bar and nothing else, despite the console showing the relevant elements loading. The menu bar is not clickable (see reply post below for image).

  • Here’s the weird part: if I add an error after the call to open_form, then everything works.

Curious to hear if anyone (@ian / @meredydd) may have a guess about what’s going on. Thanks!

-sam

@not_sam – This isn’t a cookie thing; it’s because you’re calling open_form() in a form constructor. The moment that constructor returns, the form it’s building is going to be opened on the screen, so your code opens _play for a split second and then immediately opens _login. If you want to redirect from one screen to another, use the show event: when that event fires, your form is already on the screen, so it’s safe to call open_form() on something else.

1 Like

Aha! That explains everything. Thanks for the prompt reply.

2 posts were split to a new topic: Cookies and Session Recovery