# 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 33

### Methods

#### (static) associateWithAuthData(userObj, platform, authData) → {Promise}

Source:
  - user.js, line 794

Associate a user with a third party auth data(AccessToken).

##### Example

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

##### Parameters:

Name | Type | Description
--- | --- | ---
`userObj` | AV.User | A user which you want to associate.
`platform` | string | Available platform for sign up.
`authData` | Object | The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }

##### Returns:

A promise that is fulfilled with the user when completed.

Type
  Promise

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

Source:
  - user.js, line 664

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 1014

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 965

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 841
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 825
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 649
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 740
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 699
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 773
Since:
  - 2.0.0

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

##### Returns:

Type
  AV.User

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

Source:
  - user.js, line 803

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 876

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(mobilePhone) → {Promise}

Source:
  - user.js, line 953

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 | Description
--- | --- | ---
`mobilePhone` | String | The mobile phone number associated with the user that want to login by AV.User.logInWithMobilePhoneSmsCode

##### Returns:

Type
  Promise

#### (static) requestMobilePhoneVerify(mobilePhone) → {Promise}

Source:
  - user.js, line 892

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 | Description
--- | --- | ---
`mobilePhone` | String | The mobile phone number associated with the user that doesn't verify their mobile phone number.

##### Returns:

Type
  Promise

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

Source:
  - user.js, line 860

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(mobilePhone) → {Promise}

Source:
  - user.js, line 909

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 | Description
--- | --- | ---
`mobilePhone` | String | The mobile phone number associated with the user that doesn't verify their mobile phone number.

##### Returns:

Type
  Promise

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

Source:
  - user.js, line 924

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 629
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 762
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(authData, platform).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) signUpOrlogInWithMobilePhone(mobilePhoneNumber, smsCode, attributes, options) → {Promise}

Source:
  - user.js, line 719
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 938

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

#### authenticated()

Source:
  - user.js, line 551
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.

#### fetch()

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

#### follow(target, options)

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

Follow a user

##### Parameters:

Name | Type | Description
--- | --- | ---
`target` | AV.User | String | The target user or user's objectId to follow.
`options` | AuthOptions |

#### followeeQuery()

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

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

#### followerQuery()

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

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

#### getEmail() → {String}

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

Returns get("email").

##### Returns:

Type
  String

#### getMobilePhoneNumber() → {String}

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

Returns get("mobilePhoneNumber").

##### Returns:

Type
  String

#### getRoles(optionsopt) → {Promise}

Source:
  - user.js, line 592

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

#### getSessionToken() → {String}

Source:
  - user.js, line 582

Get sessionToken of current user.

##### Returns:

sessionToken

Type
  String

#### getUsername() → {String}

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

Returns get("username").

##### Returns:

Type
  String

#### isAuthenticated()

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

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

##### Returns:

Promise.<Boolean>

#### isCurrent()

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

Returns true if `current` would return this user.

#### linkWithWeapp() → {AV.User}

Source:
  - user.js, line 168

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

##### Returns:

Type
  AV.User

#### logIn() → {Promise}

Source:
  - user.js, line 339
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

#### save()

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

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

Source:
  - user.js, line 541
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 499
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 521
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 510
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 252
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 294
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(target, options)

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

Unfollow a user.

##### Parameters:

Name | Type | Description
--- | --- | ---
`target` | AV.User | String | The target user or user's objectId to unfollow.
`options` | AuthOptions |

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

Source:
  - user.js, line 456

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 |