{% raw %}
## Accounts
要增加賬戶功能,用`meteor add`添加下面的一個或多個包:
* `accounts-ui`:這個包允許你通過在模板中使用`{{> loginButtons}}`,來添加自動生成的登錄UI, 用戶可以登錄。社區中有其它的替代選擇,或者你也可以結合使用[advanced Accounts methods](#accounts)
* `accounts-password`: 這個包允許用戶通過密碼登錄。添加之后,`loginButtons`下拉框會自動增加郵箱和密碼文本域。
* `accounts-facebook`, `accounts-google`, `accounts-github`, `accounts-twitter`, 以及其它由社區貢獻的第三方登錄包,讓你的用戶可以通過第三方網站登錄。 它們會自動添加登錄按鈕到`loginButtons`下拉框中。
### [{{> loginButtons}}](#b-loginButtons) Client
在HTML中引入`loginButtions`模板,就可以使用Meteor默認的登錄UI。使用前,需要先添加`accounts-ui`包:
```
$ meteor add accounts-ui
```
### Anywhere but publish functions[Meteor.user()](#/basic/Meteor-user)
Get the current user record, or `null` if no user is logged in. A reactive data source.
從 [`Meteor.users`](#meteor_users) 集合中獲取當前登錄用戶。等同于`Meteor.users.findOne(Meteor.userId())`。
### Anywhere but publish functions[Meteor.userId()](#/basic/Meteor-userId)
Get the current user id, or `null` if no user is logged in. A reactive data source.
### [Meteor.users](#/basic/Meteor-users)
Anywhere
A [Mongo.Collection](#collections) containing user documents.
這個集合包含了所有注冊用戶,每個用戶是一個文檔。例如:
```
{
_id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f", // Meteor.userId()
username: "cool_kid_13", // unique name
emails: [
// each email address can only belong to one user.
{ address: "cool@example.com", verified: true },
{ address: "another@different.com", verified: false }
],
createdAt: Wed Aug 21 2013 15:16:52 GMT-0700 (PDT),
profile: {
// The profile is writable by the user by default.
name: "Joe Schmoe"
},
services: {
facebook: {
id: "709050", // facebook id
accessToken: "AAACCgdX7G2...AbV9AZDZD"
},
resume: {
loginTokens: [
{ token: "97e8c205-c7e4-47c9-9bea-8e2ccc0694cd",
when: 1349761684048 }
]
}
}
}
```
一個用戶文檔可以包含任何你想保存的用戶相關的數據。不過,Meteor會特殊對待下面的幾個字段:
* `username`: 一個唯一的字符串,可以標識用戶。
* `emails`: 一個對象的數組。對象包含屬性 `address` 和 `verified` 。一個郵箱地址只能屬于一個用戶。`verified`是一個布爾值,如果用戶已經[驗證 郵箱地址](#accounts_verifyemail)則為true。
* `createdAt`: 用戶文檔創建時間。
* `profile`: 一個對象,默認情況下用戶可以用任何數據新建和更新該字段。
* `services`: 包含第三方登錄服務使用的數據的對象。例如,它的`reset`字段包含的token,用于 [忘記密碼](#accounts_forgotpassword)的超鏈接,它的`resume`字段包含的token,用于維持用戶登錄狀態。
和所有的[Mongo.Collection](#collections)一樣,在服務端,你可以獲取用戶集合 的所有文檔,但是在客戶端只能獲取那些服務端發布的文檔。
默認情況下,當前用戶的`username`,`emails`,和`profile`會發布到客戶端。 可以使用下面的代碼發布當前用戶的其它字段:
```
// server
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId},
{fields: {'other': 1, 'things': 1}});
} else {
this.ready();
}
});
// client
Meteor.subscribe("userData");
```
如果安裝了autopublish包,那么所有用戶的信息都會發布到所有客戶端。包括`username`, `profile` ,以及`service`中所有可以公開的字段(例如:`services.facebook.id`, `services.twitter.screenName`)。另外,使用autopublish時,對于當前登錄用戶會發布更多的信息, 包括access token。這樣就可以直接從客戶端發起API調用。
默認情況下,用戶可以通過[`Accounts.createUser`](#accounts_createuser)聲明自己的`profile`字段, 也可以通過`Meteor.users.update`來修改它。要允許用戶修改更多的字段,使用[`Meteor.users.allow`](#allow) ,要禁止用戶對自己的文檔做任何修改,使用:
```
Meteor.users.deny({update: function () { return true; }});
```
### [{{ currentUser }}](#/basic/currentUser)
Calls [Meteor.user()](#meteor_user). Use `{{#if currentUser}}` to check whether the user is logged in.
{% endraw %}