Search code examples
node.jsloopbackjsstrongloop

Social Logins authentication through loopback-passport


I am starting to gain grounds with loopback api. Currently I am trying to integrate authentication through social login for my app. I have found three pages that indicate how to accommplish this but they all show this a bit differently and unclear: github-loopback-component-passport & npmjs-loopback-component-passport & github-loopback-component-passport-example. I am bit confused as to what is the most up to date process. Can anyone shed some light in how to best integrate social login with loopback? Also, how to test it for routes that may require access tokens? Here is the Github Repo of the project.

Current Dependencies

"dependencies": {
    "compression": "^1.0.3",
    "cors": "^2.5.2",
    "lodash": "^3.10.1",
    "loopback": "^2.26.2",
    "loopback-boot": "^2.6.5",
    "loopback-component-explorer": "^2.1.0",
    "loopback-connector-mysql": "^2.2.0",
    "loopback-datasource-juggler": "^2.19.0",
    "loopback-stormpath": "0.0.1",
    "serve-favicon": "^2.0.1",
    "passport": "^0.3.2",
    "underscore": "^1.8.2"
  }

Solution

  • According to the documentation provided here at -

    https://www.npmjs.com/package/loopback-passport

    You just have to install the loopback-passport npm module and add the following things to your project -

    1) providers.json - This will define the configuration for third party auth providers you want to use like facebook, google plus etc. 2) In your server.js define passport default config as shown in the link like this -

    var loopback = require('loopback');
    var path = require('path');
    var app = module.exports = loopback();
    
    // Create an instance of PassportConfigurator with the app instance 
    var PassportConfigurator = require('loopback-passport').PassportConfigurator;
    var passportConfigurator = new PassportConfigurator(app);
    
    app.boot(__dirname);
    
    ...
    
    // Enable http session 
    app.use(loopback.session({ secret: 'keyboard cat' }));
    
    // Load the provider configurations 
    var config = {};
    try {
      config = require('./providers.json');
    } catch(err) {
      console.error('Please configure your passport strategy in `providers.json`.');
      console.error('Copy `providers.json.template` to `providers.json` and replace the clientID/clientSecret values with your own.');
      process.exit(1);
    }
    
    // Initialize passport 
    passportConfigurator.init();
    
    // Set up related models 
    passportConfigurator.setupModels({
      userModel: app.models.user,
      userIdentityModel: app.models.userIdentity,
      userCredentialModel: app.models.userCredential
    });
    
    // Configure passport strategies for third party auth providers 
    for(var s in config) {
      var c = config[s];
      c.session = c.session !== false;
      passportConfigurator.configureProvider(s, c);
    }
    

    After this you can check out the different client sdks provided to make requests for login or social authentication and use them for logging and registering users.