2015-03-25 38 views
2

我有一個表單,用戶在連接表單中輸入他們的電子郵件地址和密碼。這創建了帳戶,但我現在想要進一步開發它。流星:創建帳戶之前,Meteor.users收集中不存在檢查郵件

client.js

Template.joinForm.events({ 
'submit .form-join': function(e, t) { 
    e.preventDefault(); 
    var email = t.find('#email').value, 
     password = t.find('#password').value, 
     username = Random.id(), 
     array = [], 
     profile = { 
      nameOfArray: array 
     }; 

    Accounts.createUser({ 
     email: email, 
     username: username, 
     password: password, 
     profile: profile 
    }, function(error) { 
     if (error) { 
      alert(error); 
     } else { 
      Router.go('/'); 
     } 
    }); 
} 
}); 

之前創建一個用戶帳號,你怎麼了:

1,檢查從joinForm「電子郵件」變量尚不存在的流星。用戶收集(在服務器上處理)?

  1. 如果'email'確實存在,那麼拒絕用戶創建?

我已經看到了新的功能,並想知道我是否使用它? http://docs.meteor.com/#/full/accounts_validatenewuser

Accounts.validateNewUser(function (user) { 
    //pseudo if statement code 
    if (email not exist) { 
     1. create the user account and then 
     Accounts.sendVerificationEmail(userId, [email]) 
    } else { 
      throw new Meteor.Error(403, "email address is already registered"); 
    } 
}); 

感謝您閱讀此。

我不清楚是否使用Accounts.createUser或Accounts.onCreateUser和 應該是客戶上的代碼,其中服務器上。我的目標是安全地建立帳戶,因此,在此過程中從控制檯拒絕任何其他修改權限。

回答

0

Accounts.validateNewUser用於檢查用戶對象的字段是否符合所需的格式,並相應地返回true或false。

要檢查電子郵件是否已經註冊,我認爲您應該在Accounts.onCreateUser函數(http://docs.meteor.com/#/full/accounts_oncreateuser)中包含此驗證。

無需測試的代碼,你可以嘗試這樣的事:

Accounts.validateNewUser(function (user) { 
    // Check compliance of the user object fields, using check http://docs.meteor.com/#/full/check 
}); 

Accounts.onCreateUser(function(options, user) { 
     if (options.profile){ 
      user.profile = options.profile; 
     } 

     if (Meteor.users.find({email: user.email}).fetch == 0) { 
      if(Accounts.validateNewUser(user)){ 
       Accounts.sendVerificationEmail(userId, [email]); 
       return user; 
      } else { 
       throw new Meteor.Error(403, "Error checking user fields"); 
     } else { 
     throw new Meteor.Error(403, "email address is already registered"); 
     }  
}  
1

Accounts.validateNewUser功能需要用戶提交後,以驗證他們的電子郵件。這基本上就是在註冊某件事物之後,在您登錄之前,您必須輸入通過電子郵件或移動設備發送給您的代碼 - 基本上,可以確保用戶是他們自稱的用戶。這可能會阻止您使用電子郵件[email protected]_not_a_real_place.com註冊。

如果我正在閱讀您的問題,您更關心查看電子郵件是否與查看用戶是否擁有該電子郵件帳戶相比是唯一的。您可以使用在服務器上運行的Accounts.onCreateUser來執行此操作:

每當創建新用戶時調用。返回新的用戶對象,或拋出一個錯誤來中止創建。

整個過程看起來像這樣。在客戶端,正是你有:

Template.joinForm.events({ 
'submit .form-join': function(e, t) { 
    e.preventDefault(); 
    var email = t.find('#email').value, 
     password = t.find('#password').value, 
     username = Random.id(), 
     array = [], 
     profile = { 
      nameOfArray: array 
     }; 

    Accounts.createUser({ 
     email: email, 
     username: username, 
     password: password, 
     profile: profile 
    }, function(error) { 
     if (error) { 
      alert(error); 
     } else { 
      Router.go('/'); 
     } 
    }); 
    } 
}); 

然後,在服務器上,實際上是建立在用戶面前,它會通過你的onCreateUser功能運行的用戶,不管你回來會被插入到用戶集合:

Accounts.onCreateUser(function(options, user) { 
    var email = user.emails[0]; 
    if (!email) { throw new Meteor.Error("You must provide a non-empty email"); // this may actually not be necessary -- createUser might do it for you 
    if (Meteor.users.find({emails: email}) { 
    throw new Meteor.Error("A user with email " + email + " already exists!"); 
    } 

    ... // whatever other transformation you might want 
    return user; 
}); 

您還可以檢查出accounts-ui package,因爲這取決於你想要多少就從香草實現用戶帳戶的變化,很多可能已經爲你做。

+0

卡森,你是對的。驗證需要在Accounts.onCreateUser中完成。 – meteorBuzz 2015-03-26 18:36:16

5

如果允許創建帳戶,即傳遞validateNewUser函數,現在在服務器上創建額外的空數組'nameOfArray'。當然,你可以添加更多的驗證檢查,例如密碼長度。

client.js

Template.joinForm.events({ 
'submit .form-join': function(e, t) { 
    e.preventDefault(); 
    var email = t.find('#email').value, 
     password = t.find('#password').value, 
     username = Random.id() 

    Accounts.createUser({ 
     email: email, 
     username: username, 
     password: password, 
     profile: profile 
    }, function(error) { 
     if (error) { 
      alert(error.reason); 
     } else { 
      Router.go('/'); 
     } 
    }); 
} 
}); 

server.js

Accounts.onCreateUser(function(options, user){ 

    var newEmail = user.emails[0].address 

    console.log(newEmail) 

    var emailAlreadyExist = Meteor.users.find({"emails.address": newEmail}, {limit: 1}).count()>0 

    console.log(emailAlreadyExist + ' already exists') 

    if(emailAlreadyExist === true) { 
     throw new Meteor.Error(403, "email already registered"); 
    } 
    else { 

     profile = options.profile 

     profile.nameOfArray =[] 


     user.profile = profile 

     return user 
    } 

}) 
+0

你能解釋一下這部分'profile = options.profile'' profile.nameOfArray = []''user.profile = profile'嗎? – Edgar 2016-02-28 23:36:39

3

我發現Accounts.createUser有它的建成和檢查已有的電子郵件/登錄自己的驗證。

Meteor docs: Accounts.createUser:

如果有一個用戶名或電子郵件只在 不同的情況下,現有用戶的createUser將失敗。

因此Accounts.onCreateUser甚至不會觸發,如果Accounts.createUser電子郵件/登錄驗證拋出錯誤。