expressjs + mongoose + passportjs GithubStrategy

时间:2012-09-13 23:23:52

标签: node.js express github mongoose passport.js

我正在尝试将通过GitHub进行身份验证的用户保存到mongodb中。我正在使用猫鼬。以下是我的内容:

var accountSchema = new Schema({
  firstName: String,
  lastName: String,
  email: String,
  githubId: String,
  githubAccessToken: String
});

passport.use(new GitHubStrategy({
    clientID: config.GITHUB_CLIENT_ID,
    clientSecret: config.GITHUB_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1/auth/github/callback",
    scope: ['user', 'public_repo', 'repo', 'gist']
  },
  function(accessToken, refreshToken, profile, done) {
     User.findOrCreate(...) // User is not defined
  });
}));

如何将用户保存到mongodb,这样他们每次访问网站时都不需要进行身份验证? passport-github上的示例不会将任何内容保存到数据库中,这就是我要做的事情?有没有关于此的教程?

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助: How to deal with async. findOrCreate method for passport and mongoose

基本上你必须定义一个函数(在你的情况下是findOrCreate函数)来处理数据库操作并知道必要的mongoose模式和模型。