2017-11-11 193 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

相關問題