# User

## AV. User

An AV.User object is a local representation of a user persisted to the LeanCloud server. This class is a subclass of an AV.Object, and retains the same functionality of an AV.Object, but also extends it with various user specific methods, like authentication, signing up, and validation of uniqueness.

## Constructor

#### new User()

Source:
  - user.js, line 51

### Methods

#### (static) become(sessionToken) → {Promise}

Source:
  - user.js, line 804

Logs in a user with a session token. On success, this saves the session to disk, so you can retrieve the currently logged in user using `current`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`sessionToken` | String | The sessionToken to log in with.

##### Returns:

A promise that is fulfilled with the user when the login completes.

Type
  Promise

#### (static) current() → {AV.User}

Source:
  - user.js, line 1242

Retrieves the currently logged in AVUser with a valid session, either from memory or localStorage, if necessary.

##### Returns:

The currently logged in AV.User.

Type
  AV.User

#### (static) currentAsync() → {Promise.<AV.User>}

Source:
  - user.js, line 1193

Retrieves the currently logged in AVUser with a valid session, either from memory or localStorage, if necessary.

##### Returns:

resolved with the currently logged in AV.User.

Type
  Promise.<AV.User>

#### (static) followeeQuery(userObjectId) → {AV.FriendShipQuery}

Source:
  - user.js, line 1020
Since:
  - 0.3.0

Create a followee query for special user to query the user's followees.

##### Parameters:

Name | Type | Description
--- | --- | ---
`userObjectId` | String | The user object id.

##### Returns:

Type
  AV.FriendShipQuery

#### (static) followerQuery(userObjectId) → {AV.FriendShipQuery}

Source:
  - user.js, line 1001
Since:
  - 0.3.0

Create a follower query for special user to query the user's followers.

##### Parameters:

Name | Type | Description
--- | --- | ---
`userObjectId` | String | The user object id.

##### Returns:

Type
  AV.FriendShipQuery

#### (static) logIn(username, password, options) → {Promise}

Source:
  - user.js, line 789
See:
  - AV.User#logIn

Logs in a user with a username (or email) and password. On success, this saves the session to disk, so you can retrieve the currently logged in user using `current`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`username` | String | The username (or email) to log in with.
`password` | String | The password to log in with.
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled with the user when the login completes.

Type
  Promise

#### (static) logInWithMobilePhone(mobilePhone, password, options) → {Promise}

Source:
  - user.js, line 883
See:
  - AV.User#logIn

Logs in a user with a mobile phone number and password. On success, this saves the session to disk, so you can retrieve the currently logged in user using `current`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mobilePhone` | String | The user's mobilePhoneNumber
`password` | String | The password to log in with.
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled with the user when the login completes.

Type
  Promise

#### (static) logInWithMobilePhoneSmsCode(mobilePhone, smsCode, options) → {Promise}

Source:
  - user.js, line 838
See:
  - AV.User#logIn

Logs in a user with a mobile phone number and sms code sent by AV.User.requestLoginSmsCode.On success, this saves the session to disk, so you can retrieve the currently logged in user using `current`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mobilePhone` | String | The user's mobilePhoneNumber
`smsCode` | String | The sms code sent by AV.User.requestLoginSmsCode
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled with the user when the login completes.

Type
  Promise

#### (static) loginWithWeapp() → {AV.User}

Source:
  - user.js, line 958
Since:
  - 2.0.0

使用当前使用小程序的微信用户身份注册或登录，成功后用户的 session 会在设备上持久化保存，之后可以使用 AV.User.current() 获取当前登录用户。 仅在小程序中可用。

##### Returns:

Type
  AV.User

#### (static) logOut() → {Promise}

Source:
  - user.js, line 976

Logs out the currently logged in user session. This will remove the session from disk, log out of linked services, and future calls to `current` will return `null`.

##### Returns:

Type
  Promise

#### (static) requestEmailVerify(email) → {Promise}

Source:
  - user.js, line 1063

Requests a verify email to be sent to the specified email address associated with the user account. This email allows the user to securely verify their email address on the AV site.

##### Parameters:

Name | Type | Description
--- | --- | ---
`email` | String | The email address associated with the user that doesn't verify their email address.

##### Returns:

Type
  Promise

#### (static) requestLoginSmsCode(mobilePhoneNumber, optionsopt) → {Promise}

Source:
  - user.js, line 1170

Requests a logIn sms code to be sent to the specified mobile phone number associated with the user account. This sms code allows the user to login by AV.User.logInWithMobilePhoneSmsCode function.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`mobilePhoneNumber` | String |  | The mobile phone number associated with the user that want to login by AV.User.logInWithMobilePhoneSmsCode
`options` | AuthOptions | <optional> | AuthOptions plus: ###### Properties
Name | Type | Attributes | Description
--- | --- | --- | ---
`validateToken` | String | <optional> | a validate token returned by AV.Cloud.verifyCaptcha

##### Returns:

Type
  Promise

#### (static) requestMobilePhoneVerify(mobilePhoneNumber, optionsopt) → {Promise}

Source:
  - user.js, line 1080

Requests a verify sms code to be sent to the specified mobile phone number associated with the user account. This sms code allows the user to verify their mobile phone number by calling AV.User.verifyMobilePhone

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`mobilePhoneNumber` | String |  | The mobile phone number associated with the user that doesn't verify their mobile phone number.
`options` | AuthOptions | <optional> | AuthOptions plus: ###### Properties
Name | Type | Attributes | Description
--- | --- | --- | ---
`validateToken` | String | <optional> | a validate token returned by AV.Cloud.verifyCaptcha

##### Returns:

Type
  Promise

#### (static) requestPasswordReset(email) → {Promise}

Source:
  - user.js, line 1042

Requests a password reset email to be sent to the specified email address associated with the user account. This email allows the user to securely reset their password on the AV site.

##### Parameters:

Name | Type | Description
--- | --- | ---
`email` | String | The email address associated with the user that forgot their password.

##### Returns:

Type
  Promise

#### (static) requestPasswordResetBySmsCode(mobilePhoneNumber, optionsopt) → {Promise}

Source:
  - user.js, line 1109

Requests a reset password sms code to be sent to the specified mobile phone number associated with the user account. This sms code allows the user to reset their account's password by calling AV.User.resetPasswordBySmsCode

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`mobilePhoneNumber` | String |  | The mobile phone number associated with the user that doesn't verify their mobile phone number.
`options` | AuthOptions | <optional> | AuthOptions plus: ###### Properties
Name | Type | Attributes | Description
--- | --- | --- | ---
`validateToken` | String | <optional> | a validate token returned by AV.Cloud.verifyCaptcha

##### Returns:

Type
  Promise

#### (static) resetPasswordBySmsCode(code, password) → {Promise}

Source:
  - user.js, line 1135

Makes a call to reset user's account password by sms code and new password. The sms code is sent by AV.User.requestPasswordResetBySmsCode.

##### Parameters:

Name | Type | Description
--- | --- | ---
`code` | String | The sms code sent by AV.User.Cloud.requestSmsCode
`password` | String | The new password.

##### Returns:

A promise that will be resolved with the result of the function.

Type
  Promise

#### (static) signUp(username, password, attrs, options) → {Promise}

Source:
  - user.js, line 769
See:
  - AV.User#signUp

Signs up a new user with a username (or email) and password. This will create a new AV.User on the server, and also persist the session in localStorage so that you can access the user using #current.

##### Parameters:

Name | Type | Description
--- | --- | ---
`username` | String | The username (or email) to sign up with.
`password` | String | The password to sign up with.
`attrs` | Object | Extra fields to set on the new user.
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled with the user when the signup completes.

Type
  Promise

#### (static) signUpOrlogInWithAuthData(authData, platform) → {Promise}

Source:
  - user.js, line 912
See:
  - https://leancloud.cn/docs/js_guide.html#绑定第三方平台账户

Sign up or logs in a user with a third party auth data(AccessToken). On success, this saves the session to disk, so you can retrieve the currently logged in user using `current`.

##### Example

    AV.User.signUpOrlogInWithAuthData({
      openid: 'abc123',
      access_token: '123abc',
      expires_in: 1382686496
    }, 'weixin').then(function(user) {
      //Access user here
    }).catch(function(error) {
      //console.error("error: ", error);
    });

##### Parameters:

Name | Type | Description
--- | --- | ---
`authData` | Object | The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
`platform` | string | Available platform for sign up.

##### Returns:

A promise that is fulfilled with the user when the login completes.

Type
  Promise

#### (static) signUpOrlogInWithAuthDataAndUnionId(authData, platform, unionId, unionLoginOptionsopt) → {Promise.<AV.User>}

Source:
  - user.js, line 939
Since:
  - 3.5.0

Sign up or logs in a user with a third party authData and unionId.

##### Example

    AV.User.signUpOrlogInWithAuthDataAndUnionId({
      openid: 'abc123',
      access_token: '123abc',
      expires_in: 1382686496
    }, 'weixin', 'union123', {
      unionIdPlatform: 'weixin',
      asMainAccount: false,
    }).then(function(user) {
      //Access user here
    }).catch(function(error) {
      //console.error("error: ", error);
    });

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`authData` | Object |  | The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
`platform` | string |  | Available platform for sign up.
`unionId` | string |  |
`unionLoginOptions` | Object | <optional> | ###### Properties
Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`unionIdPlatform` | string | <optional> | `'weixin'` | unionId platform
`asMainAccount` | boolean | <optional> | `false` |

##### Returns:

A promise that is fulfilled with the user when completed.

Type
  Promise.<AV.User>

#### (static) signUpOrlogInWithMobilePhone(mobilePhoneNumber, smsCode, attributes, options) → {Promise}

Source:
  - user.js, line 858
See:
  - AV.User#signUpOrlogInWithMobilePhone
  - AV.Cloud.requestSmsCode

Sign up or logs in a user with a mobilePhoneNumber and smsCode. On success, this saves the session to disk, so you can retrieve the currently logged in user using `current`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mobilePhoneNumber` | String | The user's mobilePhoneNumber.
`smsCode` | String | The sms code sent by AV.Cloud.requestSmsCode
`attributes` | Object | The user's other attributes such as username etc.
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled with the user when the login completes.

Type
  Promise

#### (static) verifyMobilePhone(code) → {Promise}

Source:
  - user.js, line 1154

Makes a call to verify sms code that sent by AV.User.Cloud.requestSmsCode If verify successfully,the user mobilePhoneVerified attribute will be true.

##### Parameters:

Name | Type | Description
--- | --- | ---
`code` | String | The sms code sent by AV.User.Cloud.requestSmsCode

##### Returns:

A promise that will be resolved with the result of the function.

Type
  Promise

#### associateWithAuthData(authData, platform) → {Promise.<AV.User>}

Source:
  - user.js, line 201
Since:
  - 3.3.0

Associate the user with a third party authData.

##### Example

    user.associateWithAuthData({
      openid: 'abc123',
      access_token: '123abc',
      expires_in: 1382686496
    }, 'weixin').then(function(user) {
      //Access user here
    }).catch(function(error) {
      //console.error("error: ", error);
    });

##### Parameters:

Name | Type | Description
--- | --- | ---
`authData` | Object | The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
`platform` | string | Available platform for sign up.

##### Returns:

A promise that is fulfilled with the user when completed.

Type
  Promise.<AV.User>

#### associateWithAuthDataAndUnionId(authData, platform, unionId, unionLoginOptionsopt) → {Promise.<AV.User>}

Source:
  - user.js, line 228
Since:
  - 3.5.0

Associate the user with a third party authData and unionId.

##### Example

    user.associateWithAuthDataAndUnionId({
      openid: 'abc123',
      access_token: '123abc',
      expires_in: 1382686496
    }, 'weixin', 'union123', {
      unionIdPlatform: 'weixin',
      asMainAccount: false,
    }).then(function(user) {
      //Access user here
    }).catch(function(error) {
      //console.error("error: ", error);
    });

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`authData` | Object |  | The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
`platform` | string |  | Available platform for sign up.
`unionId` | string |  |
`unionLoginOptions` | Object | <optional> | ###### Properties
Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`unionIdPlatform` | string | <optional> | `'weixin'` | unionId platform
`asMainAccount` | boolean | <optional> | `false` |

##### Returns:

A promise that is fulfilled with the user when completed.

Type
  Promise.<AV.User>

#### authenticated()

Source:
  - user.js, line 664
Deprecated:
  - 如果要判断当前用户的登录状态是否有效，请使用 currentUser.isAuthenticated().then()， 如果要判断该用户是否是当前登录用户，请使用 user.id === currentUser.id

Checks whether this user is the current user and has been authenticated.

##### Returns:

(Boolean) whether this user is the current user and is logged in.

#### dissociateAuthData(platform) → {Promise.<AV.User>}

Source:
  - user.js, line 258
Since:
  - 3.3.0

Unlinks a user from a service.

##### Parameters:

Name | Type | Description
--- | --- | ---
`platform` | string |

##### Returns:

Type
  Promise.<AV.User>

#### fetch()

Source:
  - user.js, line 553
See:
  - AV.Object#fetch

#### follow(options, authOptionsopt)

Source:
  - user.js, line 478
Since:
  - 0.3.0

Follow a user

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`options` | Object | AV.User | String |  | if an AV.User or string is given, it will be used as the target user. ###### Properties
Name | Type | Attributes | Description
--- | --- | --- | ---
`user` | AV.User | String |  | The target user or user's objectId to follow.
`attributes` | Object | <optional> | key-value attributes dictionary to be used as conditions of followerQuery/followeeQuery.
`authOptions` | AuthOptions | <optional> |

#### followeeQuery()

Source:
  - user.js, line 546
Since:
  - 0.3.0
See:
  - AV.User#followeeQuery

Create a followee query to query the user's followees.

#### followerQuery()

Source:
  - user.js, line 537
Since:
  - 0.3.0
See:
  - AV.User#followerQuery

Create a follower query to query the user's followers.

#### getEmail() → {String}

Source:
  - user.js, line 643
See:
  - AV.Object#get

Returns get("email").

##### Returns:

Type
  String

#### getMobilePhoneNumber() → {String}

Source:
  - user.js, line 601
See:
  - AV.Object#get

Returns get("mobilePhoneNumber").

##### Returns:

Type
  String

#### getRoles(optionsopt) → {Promise.<Array.<AV.Role>>}

Source:
  - user.js, line 732

Get this user's Roles.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`options` | AuthOptions | <optional> |

##### Returns:

A promise that is fulfilled with the roles when the query is complete.

Type
  Promise.<Array.<AV.Role>>

#### getSessionToken() → {String}

Source:
  - user.js, line 702

Get sessionToken of current user.

##### Returns:

sessionToken

Type
  String

#### getUsername() → {String}

Source:
  - user.js, line 592
See:
  - AV.Object#get

Returns get("username").

##### Returns:

Type
  String

#### isAuthenticated()

Source:
  - user.js, line 682
Since:
  - 2.0.0

检查该用户的登录状态是否有效，请注意该方法会校验 sessionToken 的有效性，是个异步方法。

##### Returns:

Promise.

#### isCurrent()

Source:
  - user.js, line 583
See:
  - AV.User#current

Returns true if `current` would return this user.

#### linkWithWeapp() → {Promise.<AV.User>}

Source:
  - user.js, line 246

将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。 仅在小程序中可用。

##### Returns:

Type
  Promise.<AV.User>

#### logIn() → {Promise}

Source:
  - user.js, line 433
See:
  - AV.User.logIn

Logs in a AV.User. On success, this saves the session to localStorage, so you can retrieve the currently logged in user using `current`.

A username and password must be set before calling logIn.

##### Returns:

A promise that is fulfilled with the user when the login is complete.

Type
  Promise

#### refreshSessionToken(optionsopt) → {Promise.<AV.User>}

Source:
  - user.js, line 712
Since:
  - 2.1.0

Refresh sessionToken of current user.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`options` | AuthOptions | <optional> |

##### Returns:

user with refreshed sessionToken

Type
  Promise.<AV.User>

#### save()

Source:
  - user.js, line 448
See:
  - AV.Object#save

#### setEmail(email, options) → {Boolean}

Source:
  - user.js, line 654
See:
  - AV.Object#set

Calls set("email", email, options) and returns the result.

##### Parameters:

Name | Type | Description
--- | --- | ---
`email` | String |
`options` | AuthOptions |

##### Returns:

Type
  Boolean

#### setMobilePhoneNumber(mobilePhoneNumber, options) → {Boolean}

Source:
  - user.js, line 612
See:
  - AV.Object#set

Calls set("mobilePhoneNumber", phoneNumber, options) and returns the result.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mobilePhoneNumber` | String |
`options` | AuthOptions |

##### Returns:

Type
  Boolean

#### setPassword(password, options) → {Boolean}

Source:
  - user.js, line 634
See:
  - AV.Object#set

Calls set("password", password, options) and returns the result.

##### Parameters:

Name | Type | Description
--- | --- | ---
`password` | String |
`options` | AuthOptions |

##### Returns:

Type
  Boolean

#### setUsername(username, options) → {Boolean}

Source:
  - user.js, line 623
See:
  - AV.Object#set

Calls set("username", username, options) and returns the result.

##### Parameters:

Name | Type | Description
--- | --- | ---
`username` | String |
`options` | AuthOptions |

##### Returns:

Type
  Boolean

#### signUp(attrs, options) → {Promise}

Source:
  - user.js, line 342
See:
  - AV.User.signUp

Signs up a new user. You should call this instead of save for new AV.Users. This will create a new AV.User on the server, and also persist the session on disk so that you can access the user using `current`.

A username and password must be set before calling signUp.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attrs` | Object | Extra fields to set on the new user, or null.
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled when the signup finishes.

Type
  Promise

#### signUpOrlogInWithMobilePhone(attrs, options) → {Promise}

Source:
  - user.js, line 386
See:
  - AV.User.signUpOrlogInWithMobilePhone
  - AV.Cloud.requestSmsCode

Signs up a new user with mobile phone and sms code. You should call this instead of save for new AV.Users. This will create a new AV.User on the server, and also persist the session on disk so that you can access the user using `current`.

A username and password must be set before calling signUp.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attrs` | Object | Extra fields to set on the new user, or null.
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled when the signup finishes.

Type
  Promise

#### unfollow(options, authOptionsopt)

Source:
  - user.js, line 513
Since:
  - 0.3.0

Unfollow a user.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`options` | Object | AV.User | String |  | if an AV.User or string is given, it will be used as the target user. ###### Properties
Name | Type | Description
--- | --- | ---
`user` | AV.User | String | The target user or user's objectId to unfollow.
`authOptions` | AuthOptions | <optional> |

#### updatePassword(oldPassword, newPassword, options)

Source:
  - user.js, line 569

Update user's new password safely based on old password.

##### Parameters:

Name | Type | Description
--- | --- | ---
`oldPassword` | String | the old password.
`newPassword` | String | the new password.
`options` | AuthOptions |