node.js - Loopback Pass用户为空

标签 node.js passport.js loopbackjs

因此,我尝试使用示例应用程序作为指导,将 Facebook 与 Loopback 和 Passport 进行基本集成: 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,并且它确实与我的数据库(在内存中)中的访问 token 匹配。

目前,我在 /auth/account 上获得重定向,转到 /loginFail。该 session 非常简单,但确实存在。 sessionID 也存在。

对我所缺少的有什么见解吗?

最佳答案

所以我找到了我的问题。为了解决这个问题,我所做的就是克隆示例,然后仔细检查以查看其中的差异。

归结为我没有将所需的模型添加到 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。它们都是内置的,但需要保存在数据库(durr)中。

只是希望这对其他人有帮助。

关于node.js - Loopback Pass用户为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37171671/

相关文章:

node.js - 如何处理没有理由抛出的已知错误

javascript - 成功登录后将用户数据从Nodejs服务器推送到Angular

node.js - Passport.js 阳性结果

javascript - 对所有字段进行环回搜索

javascript - 使用随机生成的 URL 创建临时网页

node.js - MongoDB 仅使用中间件插入 UUID?

javascript - req.isAuthenticated 总是返回 false ( react 前端)

node.js - 环回事件流跟踪服务器端模型中的更改

node.js - Loopback - 具有 "hasMany"关系的 $owner 角色

javascript - Node.js - 使用 module.exports 作为构造函数