Loopback Passport用户为空

时间:2016-05-11 19:26:18

标签: node.js passport.js loopbackjs

所以我尝试使用示例应用作为指南,与Loopback和Passport进行基本的Facebook集成: https://github.com/strongloop/loopback-example-passport

我认为我错过了一件小事,因为req.user总是回归为null,即使其他一切似乎排成一行。这会导致ensureLoggedIn(/failedLogin)每次都指向失败的登录。

这是我的server.js

var loopback = require('loopback');
var boot = require('loopback-boot');
var flash = require('express-flash');
var bodyParser = require('body-parser');

var app = module.exports = loopback();

// Creating a bit of passport
var PassportConfigurator = require('loopback-component-passport').PassportConfigurator;
var passportConfigurator = new PassportConfigurator(app);

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
  if (err) {
    throw err;
  }
});

// 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);
}

// to support JSON-encoded bodies
app.middleware('parse', bodyParser.json());
// to support URL-encoded bodies
app.middleware('parse', bodyParser.urlencoded({
  extended: true
}));

// The access token is only available after boot
app.middleware('auth', loopback.token({
  model: app.models.accessToken
}));

app.middleware('session:before', loopback.cookieParser(app.get('cookieSecret')));
app.middleware('session', loopback.session({
  secret: 'kitty',
  saveUninitialized: true,
  resave: true
}));

// We need flash messages to see passport errors
app.use(flash());

// Initialize passport
passportConfigurator.init();

// Set up related models
passportConfigurator.setupModels({
  userModel: app.models.Participant,
  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);
}

app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    var baseUrl = app.get('url').replace(/\/$/, '');
    console.log('Web server listening at: %s', baseUrl);
    if (app.get('loopback-component-explorer')) {
      var explorerPath = app.get('loopback-component-explorer').mountPath;
      console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
    }
  });
};

// start the server if `$ node server.js`
if (require.main === module) {
  app.start();
}

routes.js

var ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;

module.exports = function(app) {
  // Install a "/ping" route that returns "pong"
  app.get('/loginFail', function(req, res) {
    console.dir(req);
    res.send(req.sessionID);
  });

  app.get('/auth/account', ensureLoggedIn('/loginFail'), function (req, res, next) {
    console.log('logged IN!');
    console.dir(req);
    res.send(req.session);
  });

  app.get('/auth/logout', function (req, res, next) {
    console.log('logging out');
    req.logout();
    res.redirect('/');
  });
};

通过查看req对象,我可以看到它有一个正确的accessToken,并且它与我的数据库中的访问令牌(在内存中)匹配。

目前,我在/auth/account上获得了重定向,转到/loginFail。会议很简单,但确实存在。 sessionID也存在。

有关我失踪的内容的任何见解?

1 个答案:

答案 0 :(得分:1)

所以我想出了我的问题。我所做的就是克隆这个例子,然后用细齿梳子来看看它们之间的区别。

归结为我没有将所需的模型添加到model-config.json中:

"user": {
  "dataSource": "db",
  "public": true
},
"accessToken": {
  "dataSource": "db",
  "public": false
},
"userCredential": {
  "dataSource": "db",
  "public": false
},
"userIdentity": {
  "dataSource": "db",
  "public": false
},
"ACL": {
  "dataSource": "db",
  "public": false
},
"RoleMapping": {
  "dataSource": "db",
  "public": false
},
"Role": {
  "dataSource": "db",
  "public": false
}

我错过了ACLRoleMappingRole。它们都是内置的,但需要保存在DB(durr)中。

希望这有助于其他人。