2014-12-02 126 views
1

有人可以提供用戶創建時發送電子郵件驗證的正確方法嗎?這是重要的部分...流星:Accounts.sendVerificationEmail自定義行爲

a)我希望用戶在註冊後立即訪問。但是如果用戶在48小時內沒有點擊點擊驗證鏈接,我想拒絕他們登錄,直到他們點擊鏈接。

我的代碼到目前爲止都會發送一個電子郵件驗證,但用戶可以連續訪問應用程序,無論是否點擊驗證鏈接(所以我的代碼當然是不完整的)。

client.js

Template.join.events({ 
'submit #join-form': function(e,t){ 
e.preventDefault(); 
    var firstName= t.find('#join-firstName').value, 
    lastName= t.find('#join-lastName').value, 
    email = t.find('#join-email').value, 
    password = t.find('#join-password').value, 
    username = firstName.substring(0) + '.' + lastName.substring(0), 
    profile = { 
    fullname: firstName + ' ' + lastName 
    }; 
    Accounts.createUser({ 
    email: email, 
    username: username, 
    password: password, 
    userType: // 'reader' or 'publisher' 
    createdAt: new Date(), 
    profile: profile 
}, function(error) { 
if (error) { 
    alert(error); 
} else { 
    Router.go('home'); 
     } 
    }); 
    } 
}); 

server.js

Meteor.startup(function() { 
process.env.MAIL_URL = 'smtp://postmaster.....'; 
Accounts.emailTemplates.from = "[email protected]"; 
Accounts.emailTemplates.sitename = "My SIte Name"; 

Accounts.emailTemplates.verifyEmail.subject = function(user) { 
    return 'Please confirm tour Email address' ; 
}, 
Accounts.emailTemplates.verifyEmail.text = function(user, url) { 
    return 'Click on the link below to verify your address: ' + url; 
} 
Accounts.config({ 
    sendVerificationEmail: true 
}); 

我嘗試已通過對流星文檔自己的讀數做出和SO尋找其他的代碼。我被卡住了。感謝您的支持。

回答

2

我認爲基本的想法是有一些驗證碼例如,在其中您要檢查在用戶登錄之前每次Accounts.validateLoginAttempt。你可以做的是存儲日期&時候,用戶跡象user.profile.joinDate起來。如果用戶試圖登錄

  • 檢查的電子郵件地址已被驗證或
  • 檢查,如果用戶的48個小時
isWithinGracePeriod = function(user) { 
     ** TBD returning true or false. 
     This can be tricky when you 
     have multiple instances in 
     different time-zones. 
     ** } 

在寬限期內登錄和

Accounts.validateLoginAttempt(function(attempt){ 
    if (attempt.user && attempt.user.emails && !attempt.user.emails[0].verified) { 
    console.log('No verification action received yet.'); 
    return isWithinGracePeriod(attempt.user); 
    } 
    return true; 
}); 

而且,這裏是HTML/spacebars東西:

<body> 
    {{ > start }} 
</body> 

<template name="start"> 
    {{#if currentUser}}{{>showUserProfile}}{{else}}{{> login}}{{/if}} 
</template> 

<template name="login"> 
    ## Grab username/password here 
</template> 

如果創建了login模板,我們可以嘗試在用戶點擊確認鏈接後,捕捉到的驗證碼。需要注意的是,如果沒有用戶登錄,然後login將被渲染,所以我們通過

Template.login.created = function() { 
    if (Accounts._verifyEmailToken) { 
    Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) { 
     if (err != null) { 
     if (err.message = 'Verify email link expired [403]') { 
      var message ='Sorry this verification link has expired.'; 
      console.log(message);  
      alertBox = Blaze.renderWithData(Template.Alert, {message: message}, $("body").get(0)); 
     } 
     } else { 
     var message = "Thank you! Your email address has been confirmed."; 
     console.log(message); 
     alertBox = Blaze.renderWithData(Template.Alert, {message: message}, $("body").get(0)); 
     } 
    }); 
    } 
}; 

重視login驗證鏈接中的「鉤」發送到Accounts.createUser

Accounts.onCreateUser(function(options, user) { 
    user.profile = {}; 
    Meteor.setTimeout(function() { 
    Accounts.sendVerificationEmail(user._id); 
    }, 2 * 3000); 
    return user; 
}); 
+0

要公平48小時的寬限期不需要那麼嚴格。只要在某個時候用戶的電子郵件地址有驗證。我在Accounts.validateLoginAttempt中看到,有一個'allowed'回調函數返回true或false。首先,你能告訴我如何設置LoginWithEmail。我不明白當用戶點擊鏈接時目前的功能。 +1爲有問題的系統方法,謝謝Steffo – meteorBuzz 2014-12-02 17:12:10

+0

重新編輯答案。當用戶點擊驗證鏈接(看起來這個http:// localhost:3000 /#/ verify-email/lots-of-funny-chars)時,會創建'login'模板並且'Accounts.verifyEmail'抓住有趣的-chars並驗證它們(流星注意到這一點)。 – Steffo 2014-12-02 18:54:42

+0

感謝您的意見。我會應用這段代碼,並讓你知道我很快會如何投票並相應投票。 – meteorBuzz 2014-12-03 09:54:01