Chinese Documentation : Configuring providers.json

Overview

Use the providers.json file to configure third-party login using loopback-component-passport.  This file contains settings for each third-party authorization provider, in provider and provider-link objects (for example, google-login and google-link).

To load the configuration, add code such as the following to server.js:

/server/server.js
var loopbackPassport = require('loopback-component-passport');
var PassportConfigurator = loopbackPassport.PassportConfigurator;
var passportConfigurator = new PassportConfigurator(app);

// Build the providers/passport config
var config = {};
try {
	config = require('../providers.json');
} catch (err) {
	console.trace(err);
	process.exit(1); // fatal
}

Example providers.json

Below is the providers.template.json file provided with loopback-example-passport.

providers.template.json  Expand source
 {
  "local": {
    "provider": "local",
    "module": "passport-local",
    "usernameField": "username",
    "passwordField": "password",
    "authPath": "/auth/local",
    "successRedirect": "/auth/account"
  },
  "facebook-login": {
    "provider": "facebook",
    "module": "passport-facebook",
    "clientID": "{facebook-client-id-1}",
    "clientSecret": "{facebook-client-secret-1}",
    "callbackURL": "http://localhost:3000/auth/facebook/callback",
    "authPath": "/auth/facebook",
    "callbackPath": "/auth/facebook/callback",
    "successRedirect": "/auth/account",
    "scope": ["email"]
  },
  "google-login": {
    "provider": "google",
    "module": "passport-google-oauth",
    "strategy": "OAuth2Strategy",
    "clientID": "{google-client-id-1}",
    "clientSecret": "{google-client-secret-1}",
    "callbackURL": "http://localhost:3000/auth/google/callback",
    "authPath": "/auth/google",
    "callbackPath": "/auth/google/callback",
    "successRedirect": "/auth/account",
    "scope": ["email", "profile"]
  },
  "twitter-login": {
      "provider": "twitter",
      "authScheme": "oauth",
      "module": "passport-twitter",
      "callbackURL": "http://localhost:3000/auth/twitter/callback",
      "authPath": "/auth/twitter",
      "callbackPath": "/auth/twitter/callback",
      "successRedirect": "/auth/account",
      "consumerKey": "{twitter-consumer-key}",
      "consumerSecret": "{twitter-consumer-secret}"
  },
  "facebook-link": {
    "provider": "facebook",
    "module": "passport-facebook",
    "clientID": "{facebook-client-id-2}",
    "clientSecret": "{facebook-client-secret-2}",
    "callbackURL": "http://localhost:3000/link/facebook/callback",
    "authPath": "/link/facebook",
    "callbackPath": "/link/facebook/callback",
    "successRedirect": "/link/account",
    "scope": ["email", "user_likes"],
    "link": true
  },
  "google-link": {
    "provider": "google",
    "module": "passport-google-oauth",
    "strategy": "OAuth2Strategy",
    "clientID": "{google-client-id-2}",
    "clientSecret": "{google-client-secret-2}",
    "callbackURL": "http://localhost:3000/link/google/callback",
    "authPath": "/link/google",
    "callbackPath": "/link/google/callback",
    "successRedirect": "/link/account",
    "scope": ["email", "profile"],
    "link": true
  }
}

Provider property reference

Common properties

PropertyTypeDescriptionExampleDefault
providerString

Identifies the provider; can be any identifier string.

"facebook" 
moduleStringNode module to use"passport-facebook" 
authSchemeStringDefault is OAuth 2.0"oauth"oAuth 2.0
strategyStringThe name of passport strategy "OAuth2Strategy" 
authPathStringThe local URL for authentication"/auth/facebook"/auth/<provider>
linkBooleanTrue if you want to link accounts.truefalse

OAuth 1.0

Twitter

PropertyTypeDescriptionExample
consumerKeyStringA value used by the Consumer to identify itself to the Service Provider 
consumerSecretStringA secret used by the Consumer to establish ownership of the Consumer Key 
callbackURLStringA URL the Service Provider will use to redirect the User back to the Consumer when Obtaining User Authorization is complete"http://localhost:3000/auth/facebook/callback"
callbackPathStringA local URL to mount the callback page "/auth/facebook/callback"
successRedirectStringA local URL for the success login"/auth/account"
scopeArray of StringAn array of oAuth 1.0 scopes["email"]

OAuth 2

Google and Facebook

PropertyTypeDescriptionExample
clientIDStringThe client identifier issued to the client during the registration process 
clientSecretStringThe client secret 
callbackURLStringoAuth 2.0 callback URL"http://localhost:3000/auth/facebook/callback"
callbackPathStringA local URL to mount the callback page"/auth/facebook/callback"
successRedirectStringA local URL for the success login"/auth/account"
scopeArray of StringAn array of oAuth 2.0 scopes["email"]

Local

PropertyTypeDescriptionExampleDefault
usernameFieldStringThe field name for username on the login form"user"username
passwordFieldStringThe field name for password on the login form"pass"password
successRedirectStringA local URL for the success login"/auth/account"