2015-10-23 33 views
0

我是Meteor新手,對Collection2和Accounts有一些疑問。如何使用Collection2(服務器端)保護Accouts.createUser()

目前,用戶可以創建一個帳戶。這由一個調用Meteor方法的模板事件(客戶端)來處理。該方法定義了客戶端和服務器端。客戶端,我做一些UI的東西(沒有什麼重要的),而服務器端,我創建的帳戶。

這裏是我的服務器端流星方法:

Meteor.methods({ 

    'registerUser': function (user) { 
     Accounts.createUser(user); 
    } 

}); 

我也用Collection2包插入或更新文件之前檢查數據:

Schema = {}; 

Schema.UserProfile = new SimpleSchema({ 
    name: { 
     type: String, 
     optional: false, 
     label: "Name" 
    } 
}); 

Schema.User = new SimpleSchema({ 
    username: { 
     type: String, 
     optional: true, 
     label: "Username" 
    }, 
    emails: { 
     type: Array, 
     optional: true 
    }, 
    "emails.$": { 
     type: Object 
    }, 
    "emails.$.address": { 
     type: String, 
     regEx: SimpleSchema.RegEx.Email, 
     label: "Email address" 
    }, 
    "emails.$.verified": { 
     type: Boolean 
    }, 
    createdAt: { 
     type: Date 
    }, 
    profile: { 
     type: Schema.UserProfile, 
     optional: true 
    }, 
    services: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    } 
}); 

Meteor.users.attachSchema(Schema.User); 

好了,所以我在這裏。我的問題是在創建用戶(服務器端)時獲取由Collection2返回的潛在錯誤。類似這樣的:

Meteor.methods({ 

    'registerUser': function (user) { 
     Accounts.createUser(user, (err, res) => { 
      if(err) return err; 
     }); 
     return true; 
    } 

}); 

但是Meteor不支持Accounts.createUser()的回調。所以即使我使用Collection2,我的用戶也可以提交錯誤的輸入。

現在的問題是:在這種情況下是否有可能在帳戶中使用Collection2?

謝謝你的一切! :)

回答

0

當您撥打Accounts.createUser時,您不會傳遞與Meteor.users集合中找到的架構對象。你傳遞一個選項與指定的字段對象:

選項
用戶名字符串
該用戶的唯一名稱。

電子郵件字符串
該用戶的電子郵件地址。

密碼字符串
用戶的密碼。這不是通過電報以純文本發送的。

配置文件對象
用戶的配置文件,通常包括名稱字段。

從客戶端調用此方法確實有一個回調:

回調功能僅
客戶端,可選的回調。調用成功時沒有參數,或者在失敗時使用單個錯誤參數。

不要爲用戶對象創建架構。

雖然您可以使用Schema.UserProfile來驗證profile參數中傳遞的內容,但最好的辦法是在服務器上的accountsServer.validateNewUser回調中。

Accounts.validateNewUser(function (user) { 
    if(Match.test(user.profile, Schema.UserProfile)) 
    return true; 
    throw new Meteor.Error(403, "Invalid UserProfile"); 
}); 
+0

完美地工作。非常感謝你的幫助。 – Armelias