Hook into Anvil's built-in user sign up service to run custom code

hi @benwhut and welcome to the forum,

Sure

this is a way I’ve done it before

On my user table I add a boolean column details
I have a startup module that checks the user on the server and raises an exception depending on whether there is A) a valid user B) a valid user who has yet to complete the necessary information

Client side

def startup():
  try:
    user = anvil.server.call('check_user')
    open_form('Main')
  except anvil.users.AccountIsNotEnabled:
    open_form('UserDetails')
  except anvil.users.AuthenticationFailed:
    open_form('Login')

if __name__ == "__main__":
  startup()

server-side

@anvil.server.callable
def check_user():
  user = anvil.users.get_user()
  if user is None:
    raise anvil.users.AuthenticationFailed('user not logged in')
  if not user['details']:
    raise anvil.users.AccountIsNotEnabled('details need completing')
  return user


then in each of the Login form and the UserDetails form I recall the startup function after the obvious action.

  def login_button_click(self, **event_args):
    """This method is called when the button is clicked"""
    user = anvil.users.login_with_form(allow_cancel=True)
    if user is not None:
      from ..StartUp import startup
      startup()

  def save_button_click(self, **event_args):
    """This method is called when the button is clicked"""
    anvil.server.call('update_user_details', self.text_box.text)
    from ..StartUp import startup
    startup()
3 Likes