2014-09-03 111 views
2

我正在構建Meteor應用程序,並且需要在用戶創建帳戶後刪除流星的自動註冊。如何在註冊流星後創建沒有自動註冊的用戶

我正在使用用戶界面的帳戶密碼和帳戶條目(可選)。

有什麼想法?謝謝。

+0

可能重複[如何防止創建用戶後自動登錄](http://stackoverflow.com/questions/17360037/how-to-prevent-auto-login-after-create-user) – 2014-09-03 10:57:29

+0

是的,它是重複的。對不起,我在創建之前還沒有找到它。謝謝! – skozz 2014-09-03 11:03:58

回答

1

您可以使用下面的代碼:

設置一個firstLogin標誌上創建

Accounts.onCreateUser(function(options, user) { 
    user.firstLogin = true; 
    return user; 
}); 

方法來更新標誌

Meteor.methods({ 
    updateUserFirstLogin: function(userId) { 
    Meteor.users.update({ 
     _id: userId 
    }, { 
     $set: { 
     'firstLogin': false 
     } 
    }); 
    } 
}); 

檢查,如果用戶在登錄一個新

Accounts.validateLoginAttempt(function(attemptInfo) { 
    if (!attemptInfo.user) { 
    return false; 
    } 
    if (!attemptInfo.user.firstLogin) { 
    return true; 
    } else { 
    Meteor.call('updateUserFirstLogin', attemptInfo.user._id); 
    return false; 
    } 
}); 
8

這是通過電子郵件登錄一個簡單的解決方案,它將解除用戶創建後autologins直到電子郵件地址驗證拒絕後登錄:

if (Meteor.isServer) { 

    Accounts.validateLoginAttempt(function(attemptInfo) { 

     if (attemptInfo.type == 'resume') return true; 

     if (attemptInfo.methodName == 'createUser') return false; 

     if (attemptInfo.methodName == 'login' && attemptInfo.allowed) { 
      var verified = false; 
      var email = attemptInfo.methodArguments[0].user.email; 
      attemptInfo.user.emails.forEach(function(value, index) { 
       if (email == value.address && value.verified) verified = true; 
      }); 
      if (!verified) throw new Meteor.Error(403, 'Verify Email first!'); 
     } 

     return true; 
    }); 

} 
+0

問題在於,代碼不會創建用戶。因爲這個validateLoginAttemp()以某種方式在createUser()之前運行。當它在第二秒返回false時,流星不會註冊新用戶,既不發送電子郵件確認。 – 2017-10-27 16:44:05

0

我發現了一個更簡單的方法:

Accounts.validateLoginAttempt((data) => { 
    let diff = new Date() - new Date(data.user.createdAt); 
    if (diff < 2000) { 
     console.info('New user created -- denying autologin.'); 
     return false; 
    } else { 
     return true; 
    } 
}); 

這看到用戶剛剛創建,因此不會登錄。