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

Hi!

I’m happy with Anvil’s built-in user sign-up/authentication service. It works nicely!

But is there a way that I can hook into this service? I want to execute some custom code whenever there is a successful new sign up. This code would only run one time when a user first signs up.

I’m trying to avoid having to create my own custom user auth as described here:
https://anvil.works/docs/how-to/custom-user-auth
since I don’t really want to change anything with how the user auth works.

Is there a simple way I can trigger my own custom code whenever there is a successful new user registration using Anvil’s built-in user authentication service?

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

Thanks @stucork for the quick reply and solution… even complete with sample code! :+1:

Your solution looks good. I will try it out now.

Thanks again for your help!!