2016-10-20 49 views
1

本地機器上的流星網絡應用使用Accounts-passwordAccounts-ui
用戶輸入電子郵件和密碼以創建新帳戶。流星帳戶 - 用戶訪問前等待郵件驗證

它需要進行配置,以便用戶只有在通過電子郵件驗證帳戶的創建之後才能獲取userId(從而成爲currentUser)。

該應用程序有一個smtp變量設置。它發送驗證電子郵件罰款。但未能在驗證之前阻止用戶「登錄」。如何製作它,以便在驗證後僅提供有效的userId?由於

//server 
Accounts.onCreateUser(function(options, user) { 
    Accounts.config({ 
    sendVerificationEmail: true 
    }); 
    return user; 
}); 
+0

首先,每次創建用戶時都不需要調用Accounts.config()。登錄的控制可以通過['Accounts.validateLoginAttempt']完成(http://docs.meteor.com/api/accounts-multi.html#AccountsServer-validateLoginAttempt)。 – MasterAM

+0

@MasterAM是,而不是onCreateUser? –

+0

這些是2種不同的API。您應該閱讀文檔以瞭解您可以執行的操作。比猜測好。你說你想阻止登錄,所以這是你需要定位的API調用。 – MasterAM

回答

1

我定義一個全局的幫手:

Template.registerHelper('isVerified',function(){ // return a true or false depending on whether the referenced user's email has been verified 
    if (Meteor.user() && Meteor.user().emails) return Meteor.user().emails[0].verified; // look at the current user 
    else return false; 
}); 

然後在任何模板(通常是我的主模板),我可以這樣做:

{{#if isVerified}} 
    content that only verified users should see 
{{else}} 
    Please check your email for your verification link! 
{{/if}} 
+0

這段代碼的一個問題是,如果沒有記錄存在用戶,它會運行'{{else}}'塊。 –

+0

該方法的問題在於繞過相當容易。這是否足夠取決於電子郵件驗證的重要性(如果這只是「微調」以提醒用戶驗證電子郵件,那是合理的)。 – MasterAM

1

這不是很簡單了這一切。流星需要一個用戶有一個ID來發送給他們一封電子郵件。你想要做的事可能不是實際上阻止登錄,但阻止爲了做你想做的事情,當他們登錄到時,你將不得不阻止他們看到任何東西。在你的頂級模板:

{{#if userVerified}} 
    // Body of your app here 
    {{> Template.dynamic template=main}} 
{{else}} 
    You are not yet verified 
{{/if}} 

這在相應的.js文件:

Template.body.helpers({ 
    userVerified() { 
    const user = Meteor.user(); 
    return user.emails[0].verified; 
    } 
}); 
+0

user.emails.verified = false,但'{{ #if currentUser}}'返回true。怎麼來的? –

+0

D'oh!它返回true,因爲它們已經登錄。我更新了代碼片段,只有在它們被驗證的情況下才返回true。 –

1

由於@Season指出,驗證需要用戶ID,以便將其鏈接到驗證郵件中創建。

您可以做的是防止用戶在未驗證電子郵件的情況下登錄。看到這個回答另一個問題,它可以實現此目的:

https://stackoverflow.com/a/24940581/3512709