2017-11-11 260 views
1

我写了我的自定义方法登录,但我在最后一步被阻止:客户端的有效登录。流星 - 成功登录服务器端后返回客户端

我相信我正确地记录在服务器端,但没有客户端:

  • 我在DB得到了充分&正确(时间戳相干)LoginTokenswhen & hashedToken)。
  • 在minimongo,我有机会获得所有文件,至少我是店主(this.userId)。
  • 允许登录尝试Accounts.validateLoginAttempt(function (attempt),包含正确的用户并且不返回任何错误。
  • 在客户机上通话的回归,Meteor.loggingIn()falseMeteor.user()null
  • 在服务器Accounts.onLogin(function(user)返回细末user._id

所以我想这是对返回给客户端(如用户的问题。 _id) - 但我迷路了,认为我需要一位经验丰富的批评家的眼睛。

PS:我有[email protected] & [email protected]

登录方法(从客户端通常称为)

Meteor.methods({ 

    logTwo (userfinal, passfinal) { 

     // Consistency var check 
     check(userfinal, String); 
     const passwordValidator = {digest: String, algorithm: String}; 
     check(passfinal, passwordValidator); 

     // check user 
     const getUser = Accounts.findUserByEmail(userfinal); 
     if (!getUser) {throw invalidLogin();} 

     // check password 
     const checkPassword = Accounts._checkPassword(getUser, passfinal); 
     if (checkPassword.error) {throw invalidLogin();} 

     // get user's id 
     var userID = getUser._id 

     // logic here 

     console.log('code verified'); // rightly printed 
     // below, I tried with or without methodArguments (this, 'login', {user: userfinal,password: passfinal}, 
     // and (this, 'login', '', 
     Accounts._attemptLogin(this, 'login', {user: userfinal,password: passfinal}, { 
      type: '2FALogin', 
      userId: userID, 
     }); 
    }, 
}); 

Accounts.validateLoginAttempt(function (attempt) { 
    console.log(attempt); // rightly printed 

    if (attempt.type === '2FALogin' && attempt.methodName === 'login') { 
     console.log('allowed'); // rightly printed 
     return true; 
    } 

    if (attempt.error) { 
     console.log('login error: ' + attempt.error); 
    } 

}); 

回报Accounts.validateLoginAttempt的(函数(企图)(console.log(企图))

{ type: '2FALogin', 
    allowed: true, 
    methodName: 'login', 
    methodArguments: 
    [ '[email protected]', 
    { digest: '70bd58ff28477...', // digest here ok 
     algorithm: 'sha-256' } ], 
    user: 
    { _id: '6i6vLjc8Ssg6SGJNf', 
    createdAt: 2017-11-01T15:08:52.332Z, 
    services: { password: [Object], resume: [Object] }, 
    emails: [ [Object], [Object] ], 
    _loggedIn: true, 
    }, 
    connection: 
    { id: 'xFLv3XZWztxsdxckM', 
    close: [Function: close], 
    onClose: [Function: onClose], 
    clientAddress: '127.0.0.1', 
    httpHeaders: 
     { 'x-forwarded-for': '127.0.0.1', 
     'x-forwarded-proto': 'ws', 
     host: 'localhost:3000', 
     'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36', 
     'accept-language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6' } } } 
+1

您是否尝试过的方法调用'Meteor.loginWithPassword'当一切都已经验证?在方法中也可能值得尝试'this.setUserId'? –

+0

我想这只是一个计时问题。我想,在你的方法回调被调用的时候,用户文档还没有发布到客户端,因此尚不可用。 – MasterAM

+0

@MasterAM如何解决它? – Ontokrat

回答

1

我想如何管理它。

  1. Meteor.loginWithPassword不是一种选择,因为它内部没有一个Meteor.callsource
  2. 我随叫随到的回归成功Meteor.connection.setUserId(response)尝试工作,但没有被存储在localStorage:所以每隔更新,我被注销。

我需要Accounts.callLoginMethod,从accounts-base

,其在成功呼叫 this.setUserId(id)Accounts._setLoginToken在服务器上并返回与 字段 id(包含用户ID), token对象(含有

登录方法简历 令牌)以及可选的tokenExpires

此外,在该方法中,我需要回报功能Accounts._attemptLogin(或没有什么可以由客户端处理)。

因此,恢复:

在客户端

Accounts.callLoginMethod({ 
    methodName: 'logTwo', 
    methodArguments: [ 
     { 
     user: userfinal, 
     password: passfinal 
     }, 
    ], 
    userCallback: function(error) { 
     if (!error) { 
     // handle return here 
     } 
    } 
}); 

在服务器

Meteor.methods({ 

    logTwo (options) { 

     // Consistency var check 
     const passwordValidator = {digest: String, algorithm: String}; 
     check(options, { 
      user: String, 
      password: passwordValidator 
     }); 


     // check user 
     const getUser = Accounts.findUserByEmail(options.user); 
     if (!getUser) {throw invalidLogin();} 

     // check password 
     const checkPassword = Accounts._checkPassword(getUser, options.password); 
     if (checkPassword.error) {throw invalidLogin();} 

     // get user's id 
     var userID = getUser._id 

     // logic here 

     return Accounts._attemptLogin(this, 'login', '', { 
      type: '2FALogin', 
      userId: userID, 
     }); 
    }, 
}); 

Accounts.validateLoginAttempt(function (options) { 

    if (options.type === '2FALogin' && options.methodName === 'login') { 
     return true; 
    } 

    if (options.error) { 
     console.log('login error: ' + options.error); 
    } 

}); 
+1

这东西需要放在流星指南的某个地方。你愿意写一个关于实现自定义登录方法的简短部分吗? https://github.com/meteor/docs/ –

+0

@FredStark你是对的,我会的。但也许为什么'帐户'功能不在文档中,而且一般如此记录不完善,是因为它们可以在没有通知的情况下更改。 – Ontokrat