Proper Landing Page to App Page login / logout sequencing

Hello Friends:

Following on the heels of this post, I have variation of that question.

I have a Landing Page Form and an App Page Form that I want to sequence logins / logouts between. They share no components. I have a prototype solution which I don’t like, and am posting it here hoping for assistance on improving it.

Here it is.


Here’s the trivial App Page (with a Logout button) and its Form code:

class AppForm(AppFormTemplate):
    def __init__(self, **properties):
       open_form('LandingForm')
       self.init_components(**properties)
       validate_login() # Post login_with_form() validations performed here.

    # Event handler for Logout button click.
    def btn_logout_click(self, **event_args):
       anvil.users.logout()
       open_form('LandingForm')


Here’s the trivial Landing Page (with a Login button) and its Form code:

class LandingForm(LandingFormTemplate):
  def __init__(self, **properties):
     self.init_components(**properties)
        
  def form_show(self, **properties):
     while not anvil.users.get_user():
        time.sleep(20) # Try not to laugh. LoL

  # Event handler for Login button click.
  def btn_login_click(self, **event_args):
     anvil.users.login_with_form(allow_cancel=True, allow_remembered=False)

Of course, I don’t like the time.sleep(…). SideNote: Because I’m using Magic Link logins, the number of delay-seconds (whether 20 or 200,000) doesn’t actually matter because, when the user clicks the Magic Link, form_show() runs a second time where the while / loop condition is immediately breached and never entered. This would fail with, say, Google based logins.

Any suggestions on rearranging this code to eliminate time.sleep(…) and generally improve things?

Thank you!

I would keep App Page as your “base” form where you add/remove forms as components to some card/column panel.

The Login/Logout button would then be a single button that changes text/executes different code pending on a user being logged in or not. (by changing the click event or checking the text on the button)

In App Page you can check if a user is logged in, if they are not, then you route them to a Not Logged in Page else the Landing Page.

For routing I would look into this library:

HashRouting: Routing, navigation with URL Hash

2 Likes

Hi @anthonys

Thank you! Yeah, that’s probably what I’ll end up doing (less hassle). Fortunately, one technique I did catch onto early into this inaugural Anvil project, is designing Forms that leverage turning on & off ColumnPanels and/or components within them, as well as changing button labels (etc, etc, etc) so that a Form can serve slightly different purposes depending on the state. You provide another example of this. I really do need to read the routing module you recommended, as others have pointed it out as well. It’s just the drinking from a firehose and lack of time (deadline to get something out). You know how that goes. :performing_arts:

Thanks for responding to my question. I appreciate it.

1 Like