Login and open_form questions

Hi,

I may be doing something wrong or maybe this has been answered before but I wasn’t able to find anything related to my problem.

I want to create a form that checks if the user is logged in and redirects him to a new form before the elements on the first one load or, if the user is not logged in, perform the log in with a button and automatically redirect him to the new form if the login was successful.

The first question I have is how to redirect the user right after logging in with the anvil.users.login_with_form() method?

The second issue that I am facing is navigating to a new form with open_form before loading any elements.

I tried to test it and whenever the form is located inside a event handler like a click method, it correctly opens the new form and logs the message that it was opened.

If I call the open_form method on the initialization phase or outside an event handler, it logs the message that the new form was opened but nothing changes and I am still in the first form. I even tried to create a method for it.

Do you have any suggestions?

Here is the code:

    class Home(HomeTemplate):
      
      def __init__(self, **properties):
        # Set Form properties and Data Bindings.
        self.init_components(**properties) 
        
        if anvil.users.get_user():      
          self.navigate_to_main()
          print("User already logged in.")
          
      def navigate_to_main(self):
        open_form('Test')
        pass
        
      def logIn_button_click(self, **event_args):
        """This method is called when the button is clicked"""    
        anvil.users.login_with_form()
        #open_form('Test') - this is where I want to redirect the user after logging in  
        pass

      def link_logout_click(self, **event_args):
        """This method is called when the link is clicked"""
        print("Logging Out")
        anvil.users.logout()
        pass

The Test form only has a starting print and a label inside it.

Thank you!

Check out this thread: Assign startup-form dynamically based on url hash

This explains how to find the form’s “show” event that the above thread references. Basically, you want to put such startup code in the function triggered by the form’s “show” event rather than in __init__: Trouble calling javascript function during app initialization

2 Likes

@calin_giuclea

Hi there,

Apologies if I am misunderstanding, but yes, you should be able to do this with standard Anvil features.

If you check the following box from the Users service, Anvil will “check” if the user is logged in, or log them in using the standard login form
sc

As @hugetim pointed out, if you want an immediate redirect to a new form, you will want to set the show event on a “dummy” login form. You can find it here:

sc2

Here’s an example app you can clone:

You can see here that it checks if I’m logged in (and remembers me), and redirects me to a new form upon login.

com-video-to-gif%20(9)

I hope this helps!

2 Likes

Hi,

Thank you for the quick and documented responses. They seem to answer my problem.

I will try the methods you suggested as soon as I have some spare time.

I will tell you how it went.

Thank you!

I implemented the ideas you both suggested and it works like a charm.

Thanks a lot for your help!

3 Likes