How do I control the component inside the login_with_form

What I’m trying to do:

  1. How do I configure the login_with_form login button so the user clicks on it will redirect it to the relevant form I want?

  2. I have issues with “remember login between sessions” when this turn on, the login_with_form is not accessible anymore for users who have already signed up and confirmed their emails, so I tried to switch from Home to the relevant page in the code; what happened is that it loaded to the proper form execute the print test I did and hit back automatically to the HOME page.
    I want to be able to redirect the user to a new page when it has already login and email confirmed.

  3. I want to run a relevant form without getting loaded all the time from Home, meaning I am writing code on form 2. When running the application, I want to see form2 without clicking from Home to form2 if possible.

What I’ve tried and what’s not working:

Code Sample:

# this is a formatted code snippet.
# def check_if_user_login(self):
      user = anvil.users.get_user()
      if user is not None and user['confirmed_email']:
        open_form('Chat')

**Clone link:**
*share a copy of your app*
https://anvil.works/build#clone:N6WKPXGUVFFZOCJ2=N4AFZJOWZOWP4NNUZMUDV4NB

Set module as a startup form and in def startup(). set your If statement where it will redirect user to the right form.

Sadly as far I know get_user() won’t retrive the columns of the database. It was planned to be implemented, but I think it’s not available yet. It will be necessary to use the logged user to query the users table and then use it in if statement or use anvil.users.login_with_form()
Server call for the option with get_user() will take ~2-3s unless you are on pricing plan Business or higher with persistent server

def startup():
  user = anvil.users.login_with_form()
  #option with .get_user()
  #user = anvil.users.get_user()
  #user = anvil.server.call('get_user_email_column', user)

  if user is not None and user['confirmed_email']:
    open_form("StartingForm")
  else:
    open_form("StartingForm2")
    #or elif with multiple options


startup()

Thanks KR1,
The code is executed fine; I received the user in my sample code just fine.
def check_if_user_login(self):
user = anvil. users.get_user()
if user is not None and user[‘confirmed_email’]:
open_form(‘Chat’)

The problem is with the open_form(‘Chat’)
It executes it because it prints the print test message on the ‘Chat’ form, but in the browser, I keep seeing the Homepage form, not the Chat form.
It executes the code inside, but I only see the Home form.
Maybe it is just a checkbox somewhere in anvil options or something.
I have shared the entire project so you can look at it.

Thanks
Eran

Ok it will truly execute, but if you put it in the __init__ at the top. Every single line will be too executed.
So after all you will be back to your form holding the code instead of the chat form.

So you should really consider starting from a module. That decides first which form to load or add some logic to ‘home’ form to stop processing of the init if the email is already confirmed.
image

class Home(HomeTemplate):
    def __init__(self, **properties):
        # Call the parent constructor to set up the form
        self.init_components(**properties)
        confirmed = self.check_if_user_login()
        if confirmed == False:
          # Create the flow panel to hold the buttons
          flow_panel = self.create_flow_panel()
          self.add_component(flow_panel)
          
          # Add the buttons to the flow panel
          sign_up_button = self.create_sign_up_button()
          login_button = self.create_login_button()
          flow_panel.add_component(sign_up_button)
          flow_panel.add_component(login_button)
  
          # Create and add the welcome message components
          welcome_label = self.create_header_welcome_label()
          self.add_component(welcome_label)
          title_welcome_label = self.create_title_welcome_label()
          self.add_component(title_welcome_label)
          welcome_image = self.create_welcome_image()
          self.add_component(welcome_image)
        else:
          open_form('Chat')
    def check_if_user_login(self):
      user = anvil.users.get_user()
      confirmed = False
      if user is not None and user['confirmed_email']:
        confirmed = True
      return confirmed

The if else did not solve the problem, it simply does not show the components now but it is still stuck on home.
To switch the code the server module it will not work as I am using built in client function in my code.
The weird thing is that this code :slight_smile:

def go_to_login_page(self, **event_args):
        user = anvil.users.login_with_form()
        if user is not None and user['confirmed_email']:
            open_form('Chat')

actually works, and I am entering the chat form without problem.
This code however does not work:

class Home(HomeTemplate):
    def __init__(self, **properties):
      # Call the parent constructor to set up the form
      self.init_components(**properties)
      if self.check_if_user_login():
        open_form('Chat')
      else:

and this is the function code:

def check_if_user_login(self):
      user = anvil.users.get_user()
      if user is not None and user['confirmed_email']:
          print('User is logged in')
          return True
      else:
          print('User is not logged in')
          return False```
and it prints User is logged in but refuse to switch to the Chat form.

Indeed strange. For some reason starting it the way you wanted to, your form ‘Chat’ will be shortly generated and then closed. You can see it if you put print() in init.
Even if Home init is already processed, ‘Chat’ is being switched back to Home.

In my app I usually have a Frame form with app menu and I’m showing the forms inside the card component. So in that case I don’t get any problems. All I need is to say which form should be loaded in that card.

anvil.users.login_with_form() watch out with that function. It will force the user to log in if he is not logged yet.

Maybe add module and set it as your startup Form and inside that form write your function, extended with open_form(‘Home’). In that case you start first your function checking which Form should be loaded.

That code works if you use it in a module.

from anvil import *


def startup():
  user = anvil.users.get_user()

  if user is not None and user['confirmed_email']:
    open_form("Chat")
  else:
    open_form("Home")


startup()

You cannot use open_form within a form’s init function. Move the code to any function that gets called at any other time than during the form’s init, and it will work.

2 Likes

Thank you,
I switched to routing module on the client per to KR1 suggestion

# Define the main function
def main():
  # Get the current user
  user = anvil.users.get_user()
  
  # Check if the user is not None and has a confirmed email
  if user is not None and user['confirmed_email']:
    # Print the user's last login time
    print(user['last_login'])
    # Open the Chat form
    open_form("Chat")
  else:
    # Open the Home form
    open_form("Home")

# Check if the script is being run as the main program
if __name__ == "__main__":
  # Call the main function
  main()

Thanks
Eran

1 Like