Authenticating Users with Google
Your users can log in to their Google accounts in your app. It’s easy to add Google login to the Users Service - see Users:Authentication Choices to see how.
If you want to use the Anvil Google API to present a login form just for Google accounts, this section tells you how. You may wish to do it this way if you don’t want to store your users email addresses in your app’s Users table.
Log in
To allow your users to log into your app with Google, call anvil.google.auth.login()
.
import anvil.google.auth
email_addr = anvil.google.auth.login()
print(f"User logged in as {email_addr}")
If the login succeeds, anvil.google.auth.login()
will return the user’s email address.
If the login fails, or the user cancels, anvil.google.auth.login()
will raise an exception.
Check who is logged in
To find out who is currently logged in, call anvil.google.auth.get_user_email()
. If nobody is logged
in, this returns None
.
import anvil.google.auth
# This can run on the server:
email_addr = anvil.google.auth.get_user_email()
print(f"{email_addr} is now logged in")
The get_user_email
function can be called from a Server Module, or Uplink code,
to check whether a user is authorised to perform the requested action.
Do you still have questions?
Our Community Forum is full of helpful information and Anvil experts.