Way to get the forgot password link to show up on the first login form?

Right now in my app when I login, I get an Anvil standard login form. If I type the wrong password and click Login, I get that same form but with the addition of a way to reset my password through email.

Is there a way to get that password reset link to show up without needing to fail a login first? In some use cases I want to pre-create users but force the actual user to reset their password before they can login. Telling them, “Enter any password and try to login” to get to the password reset link is a bit cumbersome.

1 Like

Hi @jshaffstall

Have you seen the Custom Sign-Up Flow example? It implements a login form using Anvil’s slightly-lower-level user management functions (login_with_email etc.) - this means you can customise it to create your own login flow.

I’ve created a modified version that:

  1. checks if your email address is registered
  2. checks if your user record has a password hash
  3. If you are registered but have no password, it shows a ‘set your password’ link.
  4. The ‘set your password’ link is just the password reset link from before - so it emails the user with a password reset email. If you don’t want the user to verify their email, you can modify this as you wish.

set-your-password

Here’s a clone link:

https://anvil.works/build#clone:4B3DCW6G3QGHQ3AK=SMUFDUF5UBARODZHIEOOUAQ4|VFZEQZGB2T7F4BS2=BWIZLOO7F2UU5EYJODSSN5BG

Can you make this do what you need?

EDIT: What customisations I made to the login form

Here’s the modifications I made. The Custom sign-in app has this server function:

@anvil.server.callable
def user_has_no_pwd(email):
  user = app_tables.users.get(email=email)
  if user and not user['password_hash']:
    return True
  return False

Which is called when the email_box changes in the LoginDialog like so:

  def email_box_change(self, **event_args):
    """This method is called when the text in this text box is edited"""
    if anvil.server.call_s('user_has_no_pwd', self.email_box.text):
      self.reset_pw_link.visible = True
    else:
      self.reset_pw_link.visible = False

And the Set your password app uses it like this:

class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    if anvil.get_url_hash():
      login_flow.do_email_confirm_or_reset()
    else:
      login_flow.login_with_form()

I’d been hoping for a parameter to the standard login, rather than something that goes straight to a custom login form. I’ll need to clone and digest what you’ve got there, before knowing how it’ll work for me.

I do like the idea of having the set password option when they type in an email that doesn’t have one set yet, that’s very cool.

Thanks!

1 Like

Good news - We’ve just released an update that makes the “Forgot password?” link appear on the standard login form every time. Hopefully this fills your need!

2 Likes

That’s awesome, and perfect for my needs. Thanks!