Add Facebook Login
In this guide you will learn how to add a login with Facebook to your application.
Prerequisites
- You need a Hanko Cloud account and a project. Learn more on how to set them up here.
- You need a Facebook developer account. To create an account visit the
Facebook developer portal and select
Login
.
Get your provider redirect URL
When creating a Facebook application, you need to provide a redirect URL that determines where the third party
provider redirects after a successful login. This redirect URL consists of the base URL of the Hanko API
and the /thirdparty/callback
endpoint.
You can always view your redirect URL in the Hanko Cloud Console:
- Sign in to cloud.hanko.io.
- Select your
Organization
. - Select your
Project
. - In the left sidebar, click
Settings
, then selectSocial connections
. - Find your redirect URL in the
Redirect URL
input.
You need the redirect URL for creating a new Facebook application in the next step.
Create a new Facebook application
- Navigate to and log in to the Facebook developer portal.
- In the top navigation, select
Apps
. - Click
Create app
in the center view container or on the top right.
- Provide your
App details
and clickNext
.
- On the
Use cases
view, selectAuthenticate and request data from users with Facebook Login
. - Click
Next
.
- On the
Business
view, selectI don't want to connect a business portfolio yet
. If you do have a business portfolio and want to connect it, select it. - Click
Next
.
- On the
Create app
view, clickGo to dashboard
.
- On the app dashboard, click
Customize adding a Facebook Login button
in theAdd and customize use cases
accordion.
- In the
Customize use case
view, selectPermissions
in the left sidebar. - Click the
add
button for theemail
permission in thePermissions
table.
- In the
Customize use case
view, selectSettings
in the left sidebar. - Under
Valid OAuth Redirect URIs
add the redirect URL you previously retrieved from the Hanko Console.
If you are testing on localhost
you do not need to add the redirect URL because all http://localhost
redirects are automatically allowed by Facebook.
Get your client ID and secret
- Navigate to the
App settings
>Basic
for your app. - Copy the
App ID
. Show
and copy theApp Secret
.
Configure credentials with Hanko
-
In the Hanko Cloud Console, navigate to your project
Settings
and selectURLs
. -
Configure a Default Redirect URL. This is the URL target in your frontend the Hanko API redirects to if an error occurs during third party sign-in. If your frontend uses the
hanko-elements
web components, this URL should be the URL of the page that embeds the web component such that errors can be processed properly by the web component. -
Configure your Allowed Redirect URLs. These are the URL targets in your frontend the Hanko API is allowed to redirect to after third party authentication was successful. If your frontend uses the
hanko-elements
web components, this URL should bethe URL of the page that embeds the web component.The allowed redirect URL supports wildcard matching through globbing:
https://*.example.com
matcheshttps://foo.example.com
andhttps://bar.example.com
.https://foo.example.com/*
matches URLs likehttps://foo.example.com/page1
andhttps://foo.example.com/page2
.- Use ** to act as a super-wildcard/match-all.
- Navigate to your project
Settings
and selectSocial connections
. - Under
Connections
find theFacebook
accordion and expand it. - Enable the provider.
- Enter the
App ID
from the previous step as theClient ID
. - Enter the
App secret
from the previous step as theClient secret
. - Click
Save
.
Frontend integration
To enable a login with Facebook in your frontend application we recommend using either our pre-built UI as
provided by the @teamhanko/hanko-elements
package or building a custom UI using
the @teamhanko/hanko-frontend-sdk
.
We recommend following one of our quickstart guides to integrate
the <hanko-auth>
component from our @teamhanko/hanko-elements
package in your frontend application .
On successful integration, the component will display a button for signing in with Facebook
in the login view
of the component.
Make sure to configure the page the web component is embedded on as your error redirect URL
as well as an
allowed redirect URL
(see the previous step).
On successful authentication with the provider, the backend issues a session cookie and the web component continues the usual component flow on success. Errors that occur during third party provider authentication are also picked up and displayed in the web component accordingly.
Publish your app
If you closely followed this guide, then your app will probably still have an Unpublished
status. You must go through
Facebook’s App review
process to publish your app.
Was this page helpful?