2013-01-08 49 views

回答

2

你可以檢查裏面Meteor.autorun():

Meteor.autorun(function() { 
    if (Meteor.userId()) { 
    console.log('The user logged in'); 
    } 
}); 
+0

有使用自動運行,重新加載頁面時(或從瀏覽器中打開一個URL結束,如果用戶已經登錄它仍然會運行自動運行的問題。 – Akshat

+0

現在我明白了,非常可惜。我嘗試了很多不同的解決方法,無濟於事:你應該提交一個功能請求 –

3

Meteor.userId()函數的結果的原始登錄API(如loginWithFacebookloginWithPassword等)有一個回調火災時,登錄完畢,但目前暫不外露通過accounts-ui。這可能會改變。

潛在的解決方法,由沃納的建議啓發,但也考慮頁面加載到:

var oldUserId = undefined; 

Meteor.autorun(function() { 
    var newUserId = Meteor.userId(); 
    if (oldUserId === null && newUserId) { 
    console.log('The user logged in'); 
    } else if (newUserId === null && oldUserId) { 
    console.log('The user logged out'); 
    } 
    oldUserId = Meteor.userId(); 
}); 
2

只給一個替代;我猴子修補了回調函數。它看起來更復雜一些,因爲credentialRequestCompleteHandler需要一個函數來返回一個函數,但除了它的一個普通的猴子補丁。堅持這個main.js或一些處理晚了,只有一次。我希望它有助於未來的參考。

var orgCallback = Accounts.oauth.credentialRequestCompleteHandler; 
Accounts.oauth.credentialRequestCompleteHandler = function(callback){ 
    return function (credentialTokenOrError) { 
     var tmpFunc = orgCallback(callback); 
     tmpFunc(credentialTokenOrError); 
     alert("do your own thing here"); 
    } 
} 
+0

這是一個很好的方法,特別是如果你想在OAuth登錄時只*運行*一次*。如果用戶通過用戶名/密碼登錄,則不會*觸發。 – kynan