0

我正在为我的快速应用程序使用 passport-github 策略。当用户设置了公共电子邮件地址时,这可以正常工作,但如果没有电子邮件,则无法正常工作!

if process.env.GITHUB_ID? and process.env.GITHUB_SEC?   
  passport.use(new GitHubStrategy
    clientID: process.env.GITHUB_ID
    clientSecret: process.env.GITHUB_SEC
    callbackURL: url+"/social/githubcallback"
  , (accessToken, refreshToken, profile, done) ->
    console.log("arguments in github strategy");
    console.log("profile.emails", profile.emails);
    console.log(profile.emails[0].value);
    console.log(arguments);
    emails = []
    for mail in profile.emails
      emails.push mail.value
    if !emails[0]
      emails[0] = profile.id
    User.findOneAndUpdate(
      "email": 
        $in: emails
    ,  
      $addToSet:
        "provider": profile.provider
    , (err, user) ->
      console.log("user arguments at github strategy");
      console.log(arguments);
      console.log("email at passport callback:", profile.emails);
      if err? then return done err, null,
        message: 'authorizationfailed',
        data: '.',
        message2: 'tryagain'
      unless user
        Name = ''
        if profile.displayName?
          Name = profile.displayName
        else 
          Name = profile.username
        User.create(
          "email": emails[0]
          "provider": [profile.provider]
          "name": Name
          "surname": ""
          "active": true
          "groups": "member"
        , (err,newUser)->

          if err? then return done err, null, 
            message: 'authorizationfailed',
            data: '.',
            message2: 'tryagain'
          done null, newUser,
            message: 'authorizationsuccess'
            data: '.'
            message2: 'welcome'
        )
      else
        done null, user,
          message: 'authorizationsuccess'
          data: '.'
          message2: 'welcome'
    )
  )

所以在我的 mongodb 中,我将用户设为:

{
    "tokenExpires" : 1374052875909,
    "tokenString" : "Ll551X6Y2z-vOKCl8vTIjB5do9qxkJHL7ObRyovxlmvxZzQV7AXYgvl4424F1CZH",
    "email" : "85080",
    "_id" : ObjectId("51e5108b562c19d921000001"),
    "provider" : [
        "github"
    ],
    "lockUntil" : 0,
    "loginAttempts" : 0,
    "groups" : "member",
    "surname" : "",
    "name" : "Norman Khine",
    "active" : true,
    "password" : "$2a$10$j3KDCQ55AZROm1ggaJU5GeOaWafSzggkINl1Sy1mpLSrWtaIJTFxC",
    "__v" : 0
}

所以如果没有提供电子邮件,我正在使用 profile.id

我想要实现的是,在我的应用程序中,我有几个联合登录选项以及本地策略,如果具有相同电子邮件的用户登录,则更新 mongodb 记录,因此我得到如下记录:

{
    "__v" : 0,
    "_id" : ObjectId("51e507f7dbaac20000000001"),
    "active" : true,
    "email" : "norman@khine.net",
    "groups" : "member",
    "lockUntil" : 0,
    "loginAttempts" : 0,
    "name" : "Norman",
    "password" : "$2a$10$BRGbsMQof9TtF62SKAAeYOEVp0mjcupuICsrXcTukYImgnrrsQJ6a",
    "provider" : [
        "persona",
        "google",
        "local"
    ],
    "surname" : "Khine",
    "tokenExpires" : 1374050774162,
    "tokenString" : "hkTVcPWwWnPeXPPm5u28v6nsHbrbp-VXpJWScTSGHD7TFt3Q2HcWjNntqUOv3WFx"
}

这是将用户写入 mongodb 的 user.coffee:

# Register new user
UserSchema.statics.register = (user, cb) ->
  self = new this(user)
  user.email = sanitize(user.email.toLowerCase().trim()).xss()
  validator.check(user.email, messages.VALIDATE_EMAIL).isEmail()
  errors = validator.getErrors()
  if errors.length
    errorString = errors.join("<br>")
    return cb(errorString)
    console.log "Registration form failed with " + errors
    #go to the signup page
    return cb(errorString, null)
  else
    @findOne
      email: user.email
    , (err, existingUser) ->
      return cb(err)  if err
      return cb("user-exists")  if existingUser
      self.save (err) ->
        return cb(err) if err
        cb null, self

从 github 注册没有公共电子邮件设置的用户的正确方法是什么?

非常感谢任何建议

4

1 回答 1

1

您需要"user:email"在声明 GithubStrategy 时添加一个范围 ( )。然后,一旦您获得授权,请使用您访问的访问令牌https://api.github.com/user/emails?access_token={your access token}...您应该得到类似的响应["user@domain.com"]...

于 2013-07-18T20:33:20.070 回答