Quickstart: Facebook Integration

Log users in using Facebook accounts

Anvil provides a one-line login function to log users in to their Facebook account. You can also add a Facebook login option to the Users Service login form.

Follow this quickstart to enable the Facebook Service and log users in with Facebook.

Create an app

Log in to Anvil and click ‘create app’. Choose the Material Design theme.

Location of the Create App button

Enable the Facebook Service

In the App Browser, click the + next to Services.

App Browser showing the plus next to Services

You’ll see a list of available services and integrations. Click on Facebook API.

Services list with Facebook highlighted

Make Facebook aware of your Anvil app

Facebook needs to know about your Anvil app in order for the OAuth login flow to work. You’ll use the Facebook for Developers console for this - it only takes a few minutes.

Run through the Linking Facebook and Anvil guide and continue from here when you’re done.

Add a Facebook Login dialog

Go to the Code View for Form1.

Add this line to the __init__ method:

    anvil.facebook.auth.login()

Now add this line below it to print the logged-in user’s email:

    print('Logged in with Facebook as: %s' % anvil.facebook.auth.get_user_email())

Run your app

Run your app.

The Anvil Designer's Run App button.

You will see a Facebook login alert:

A running Anvil app with a modal that has buttons saying 'Log In with Facebook' and 'Cancel'.

If you click the ‘Log In’ button, the Facebook login screen will open in a new window:

The Facebook Single Sign On window.

You can now login with Facebook. When you’re logged in, you’ll see your email address in the Output Panel.

The Output Panel with 'Logged in with Facebook as: shaun@anvil.works' displayed.

And that’s all there is to it!

Next up

Other SSO integrations

Similar Single Sign On login functionality is available for Microsoft and Google (and there are some other integrations for Microsoft and Google too).

Just as users can log in with their Facebook accounts using anvil.facebook.auth.login(), they can log in with Microsoft accounts using anvil.microsoft.auth.login(), and Google accounts using anvil.google.auth.login().

You can add any of these login options to the multi-purpose login form presented by the Users Service as well.

The various log-in options in the Users Service.

Want another quickstart?

Every quickstart is on the Quickstarts page.