2013-10-02 69 views
0

我正在使用meteor-roles軟件包,並靜態創建用戶[email protected][email protected],..... NOw我有註冊頁面,用戶可以在其中籤署up.Initially我通過下面的客戶端代碼中創建的帳戶,我使用coffeescritp:如何將角色分配給新創建的用戶

Accounts.createUser 
    email: email 
    password: password 

做這些,我可以創建新的用戶。現在我該如何將角色分配給新創建的用戶。我有角色 - 管理員,普通用戶和管理用戶。 做當這些

id = Meteor.userId() 
roles = "admin" 
Roles.addUsersToRoles id, roles 

我得到的錯誤「Exception while delivering result of invoking 'createUser' Error {} Error: Missing 'users' param

我如何分配角色動態。 預先感謝您。!!!

回答

2

只能在用戶使用Meteor.userId()您分配角色的ID也是當前用戶,您需要在嘗試使用Roles.addUsersToRoles(Meteor.userId(), roles)之前撥打Meteor.userLoginWith*()

I w建議創建用戶,分配角色,然後登錄。如果您在分配適當的角色,特定於角色的路由,允許功能和發佈功能之前登錄用戶,則可能無法正常工作。

我建議創建用戶:

var userId = Accounts.createUser({ 
    username: 'username', 
    email: '[email protected]', 
    password: 'passwordString', 
    profile: {} 
}); 

然後將用戶添加到適當的角色:

var roles = ['admin', 'roleName']; 
Roles.addUsersToRoles(userId, roles); 

後來終於在登錄他們的角色(一個或多個)後,已分配。

+0

在做上面的選項我只是得到以下錯誤: 「未捕獲的錯誤:缺少'用戶'參數」 –

+0

這似乎對我很好。你在客戶端或服務器上執行此操作嗎?在將其分配給角色之前,嘗試控制檯記錄userId。它是否爲空? –

+0

是我已經解決了這個問題。謝謝Patrick Coffey :) :) –

相關問題