Package Exports
- @loopback/example-passport-login
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@loopback/example-passport-login) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@loopback/example-passport-login
A tutorial for implementing authentication in LoopBack 4 using passport modules.
Overview
This example demonstrates how to use the LoopBack 4 features (like
@authenticate decorator, strategy providers, etc) with
passport strategies. It includes the local strategy as
well as oauth2 strategies to interact with external OAuth providers like
facebook, google,etc.
- Log in or Sign up into a LoopBack App using passport strategy modules
- Log in via external apps like Facebook or link those external profiles with a LoopBack user (for example, a LoopBack user can have associated facebook/google accounts to retrieve pictures).
Prerequisites
Before starting this tutorial, make sure you have Client-ids/Secrets from third party apps
Install the example locally
Run the
lb4 examplecommand to installexample-passport-loginrepository:lb4 example passport-login
change into directory and then install the required dependencies:
cd loopback4-example-passport-login && npm i
Run the application
By default the user data is stored using a memory connector and saved locally to
data/db.json
Start the application with
$ npm startTest the login scenarios
Open browser to http://localhost:3000
Scenario 1 : Sign up as a local user
- Click on
Sign Upfrom the header menu and register as a local user. - If the email provided during registration, matches with your account in facebook or google you can link those profiles with your local account.
- Click on
Loginfrom the header menu and enter registered email id and password. TheView accountpage loads with user information.
Scenario 2 : Link external profiles with a local user
- Click on
Loginfrom the header menu, You will see various buttons underOther login options. - When you click on any login option, the page is redirected to that social
app's login page. On successful login with the social app, the
View accountpage is loaded. - If the email-id registered in the social media app maches with a email-id
registered locally, then the profiles will be linked and the
View accountpage will display all thelinked accountsfor that locally registered user. - Click on Logout to log out of user session
Scenario 3 : Sign up via an external Social Media app
- Click on
Loginfrom the header menu, You will see various buttons underOther login options. - When you click on any login option, the page is redirected to that social
app's login page. On successful login with the social app, the
View accountpage is loaded. - If the email-id registered in the social media app does not match any
email-ids registered locally, then a new user is signed up.
View accountpage will display the external profile used to login under thelinked accountssection. - Click on Logout to log out of user session
Try it out with FaceBook
Create a test app and test user in FaceBook
- Login to facebook developer console: https://developers.facebook.com/apps
- Click on
My Appstab in the dashboard menu, and then selectAdd a new App - This loads the
App creationpage. Pick the platform asWebsiteand then enter app category, app name and "Site URL" (Skip the quick start) - Click
Settingstab from the left hand side navigation menu, note the "App ID" and "App Secret" and save - Click the
Rolestab from the left hand side navigation menu, then theTest userslink under it, to display a list of test users. You can also create a new test user. - On any listed test user, click the edit button to open an actions menu ->
click
Change permissions granted by this test userand add [email,manage_pages] scopes to permissions
- NOTE:
- Your app may not work if the settings are missing a contact email and/or "Site URL".
- if you are testing locally, you can simply use
localhost:[port#]as your "Site URL".
Create oauth2-providers.json
Copy
oauth2-providers.template.jsonfrom this example project's root tooauth2-providers.jsonUpdate facebook oauth2 config with the values for
clientID/clientSecretfrom your test app."facebook-login": { "provider": "facebook", "module": "passport-facebook", "clientID": "xxxxxxxxxxxxxxx", "clientSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "callbackURL": "/auth/facebook/callback", "authPath": "/auth/facebook", "callbackPath": "/auth/facebook/callback", "successRedirect": "/auth/account", "failureRedirect": "/login", "scope": ["email"], "failureFlash": true, "profileFields": ["gender", "link", "locale", "name", "timezone", "verified", "email", "updated_time"] }
The profileFields field above tells facebook details to return in profile data
after authentication. For more information regarding the providers template, see
http://loopback.io/doc/en/lb2/Configuring-providers.json.html.
Log in with Facebook
- Open your browser to the example app with,
http://localhost:3000 - Click on 'Log In' from the example app header menu
- Click on 'Log In with Facebook' button
- FaceBook login page opens, enter test user-id and password
- example app loads again on successful login
- redirect to example app will fail if facebook did not return profile with email-id
Try it out with Google
Create test credentials in google
- Login to google developer console: https://console.developers.google.com You may have to create a sample project if you are new to google developer console
- Click on
OAuth consent screenfrom the left hand side navigation menu, selectExternal, clickCreatebutton - This loads the
Applicationpage to register your app. Enter app name and check if scopes hasemailpermission - Click on
Credentialslink in the left hand side navigation menu - Then click
Create Credentialstab, and selectOAuth Client ID - This loads the
Create Client IDpage. Select application type asweb application, enter name and clickCreatebutton - A pop up loads with the created credentials. Note the displayed "Client ID" and "Client Secret" and save
Create oauth2-providers.json
Copy
oauth2-providers.template.jsonfrom this example project's root tooauth2-providers.jsonUpdate google oauth2 config with the values for
clientID/clientSecretfrom your google test app."google-login": { "provider": "google", "module": "passport-google-oauth2", "strategy": "OAuth2Strategy", "clientID": "{google-client-id}", "clientSecret": "{google-client-secret}", "callbackURL": "/api/auth/thirdparty/google/callback", "authPath": "/api/auth/thirdparty/google", "callbackPath": "/api/auth/thirdparty/google/callback", "successRedirect": "/auth/account", "failureRedirect": "/login", "scope": ["email", "profile"], "failureFlash": true }