2018-01-05 18:23:38 +00:00
|
|
|
/**
|
2017-11-17 14:22:46 +00:00
|
|
|
* @flow
|
|
|
|
* User representation wrapper
|
2017-03-02 13:09:41 +00:00
|
|
|
*/
|
2017-11-17 16:17:27 +00:00
|
|
|
import INTERNALS from '../../utils/internals';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-11-17 14:22:46 +00:00
|
|
|
|
|
|
|
import type Auth from './';
|
2018-01-25 18:25:39 +00:00
|
|
|
import type {
|
|
|
|
ActionCodeSettings,
|
|
|
|
AuthCredential,
|
|
|
|
NativeUser,
|
|
|
|
UserCredential,
|
2018-02-14 13:00:19 +00:00
|
|
|
UserInfo,
|
2018-01-25 18:25:39 +00:00
|
|
|
UserMetadata,
|
|
|
|
} from './types';
|
2017-11-17 14:22:46 +00:00
|
|
|
|
2018-01-18 09:49:11 +00:00
|
|
|
type UpdateProfile = {
|
|
|
|
displayName?: string,
|
|
|
|
photoURL?: string,
|
2018-01-25 18:25:39 +00:00
|
|
|
};
|
2018-01-18 09:49:11 +00:00
|
|
|
|
2017-02-14 11:41:27 +00:00
|
|
|
export default class User {
|
2017-11-17 14:22:46 +00:00
|
|
|
_auth: Auth;
|
|
|
|
_user: NativeUser;
|
|
|
|
|
2017-03-02 13:09:41 +00:00
|
|
|
/**
|
|
|
|
*
|
2017-11-17 14:22:46 +00:00
|
|
|
* @param auth Instance of Authentication class
|
2017-10-27 10:00:06 +00:00
|
|
|
* @param user user result object from native
|
2017-03-02 13:09:41 +00:00
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
constructor(auth: Auth, user: NativeUser) {
|
|
|
|
this._auth = auth;
|
|
|
|
this._user = user;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-17 14:22:46 +00:00
|
|
|
* PROPERTIES
|
2017-02-14 11:41:27 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get displayName(): ?string {
|
|
|
|
return this._user.displayName || null;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get email(): ?string {
|
|
|
|
return this._user.email || null;
|
2017-03-14 19:04:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get emailVerified(): boolean {
|
|
|
|
return this._user.emailVerified || false;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get isAnonymous(): boolean {
|
|
|
|
return this._user.isAnonymous || false;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 16:19:08 +00:00
|
|
|
get metadata(): UserMetadata {
|
|
|
|
return this._user.metadata;
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get phoneNumber(): ?string {
|
|
|
|
return this._user.phoneNumber || null;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get photoURL(): ?string {
|
|
|
|
return this._user.photoURL || null;
|
2017-11-09 15:18:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get providerData(): Array<UserInfo> {
|
|
|
|
return this._user.providerData;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get providerId(): string {
|
|
|
|
return this._user.providerId;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get uid(): string {
|
|
|
|
return this._user.uid;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* METHODS
|
|
|
|
*/
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
|
|
|
* Delete the current user
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
delete(): Promise<void> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
|
|
|
.delete()
|
|
|
|
.then(() => {
|
|
|
|
this._auth._setUser();
|
|
|
|
});
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 12:19:34 +00:00
|
|
|
/**
|
2017-11-17 14:22:46 +00:00
|
|
|
* get the token of current user
|
|
|
|
* @return {Promise}
|
2017-03-16 12:19:34 +00:00
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
getIdToken(forceRefresh: boolean = false): Promise<string> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._auth).getToken(forceRefresh);
|
2017-03-16 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 09:49:11 +00:00
|
|
|
/**
|
|
|
|
* get the token of current user
|
|
|
|
* @deprecated Deprecated getToken in favor of getIdToken.
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
getToken(forceRefresh: boolean = false): Promise<Object> {
|
2018-01-25 18:25:39 +00:00
|
|
|
console.warn(
|
|
|
|
'Deprecated firebase.User.prototype.getToken in favor of firebase.User.prototype.getIdToken.'
|
|
|
|
);
|
2018-01-18 09:49:11 +00:00
|
|
|
return getNativeModule(this._auth).getToken(forceRefresh);
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:26:02 +00:00
|
|
|
/**
|
2018-01-24 12:20:24 +00:00
|
|
|
* @deprecated Deprecated linkWithCredential in favor of linkAndRetrieveDataWithCredential.
|
2017-11-17 14:22:46 +00:00
|
|
|
* @param credential
|
2017-07-12 15:26:02 +00:00
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
linkWithCredential(credential: AuthCredential): Promise<User> {
|
2018-01-25 18:25:39 +00:00
|
|
|
console.warn(
|
|
|
|
'Deprecated firebase.User.prototype.linkWithCredential in favor of firebase.User.prototype.linkAndRetrieveDataWithCredential.'
|
|
|
|
);
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
2018-01-25 18:25:39 +00:00
|
|
|
.linkWithCredential(
|
|
|
|
credential.providerId,
|
|
|
|
credential.token,
|
|
|
|
credential.secret
|
|
|
|
)
|
2018-01-24 09:46:39 +00:00
|
|
|
.then(user => this._auth._setUser(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param credential
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
linkAndRetrieveDataWithCredential(
|
|
|
|
credential: AuthCredential
|
|
|
|
): Promise<UserCredential> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
2018-01-25 18:25:39 +00:00
|
|
|
.linkAndRetrieveDataWithCredential(
|
|
|
|
credential.providerId,
|
|
|
|
credential.token,
|
|
|
|
credential.secret
|
|
|
|
)
|
2018-01-24 09:46:39 +00:00
|
|
|
.then(userCredential => this._auth._setUserCredential(userCredential));
|
2017-07-12 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 00:04:47 +00:00
|
|
|
/**
|
|
|
|
* Re-authenticate a user with a third-party authentication provider
|
|
|
|
* @return {Promise} A promise resolved upon completion
|
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
reauthenticateWithCredential(credential: AuthCredential): Promise<void> {
|
2018-01-25 18:25:39 +00:00
|
|
|
console.warn(
|
|
|
|
'Deprecated firebase.User.prototype.reauthenticateWithCredential in favor of firebase.User.prototype.reauthenticateAndRetrieveDataWithCredential.'
|
|
|
|
);
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
2018-01-25 18:25:39 +00:00
|
|
|
.reauthenticateWithCredential(
|
|
|
|
credential.providerId,
|
|
|
|
credential.token,
|
|
|
|
credential.secret
|
|
|
|
)
|
|
|
|
.then(user => {
|
2018-01-24 09:46:39 +00:00
|
|
|
this._auth._setUser(user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-authenticate a user with a third-party authentication provider
|
|
|
|
* @return {Promise} A promise resolved upon completion
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
reauthenticateAndRetrieveDataWithCredential(
|
|
|
|
credential: AuthCredential
|
|
|
|
): Promise<UserCredential> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
2018-01-25 18:25:39 +00:00
|
|
|
.reauthenticateAndRetrieveDataWithCredential(
|
|
|
|
credential.providerId,
|
|
|
|
credential.token,
|
|
|
|
credential.secret
|
|
|
|
)
|
2018-01-24 09:46:39 +00:00
|
|
|
.then(userCredential => this._auth._setUserCredential(userCredential));
|
2017-03-18 00:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
|
|
|
* Reload the current user
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
reload(): Promise<void> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
|
|
|
.reload()
|
2018-01-25 18:25:39 +00:00
|
|
|
.then(user => {
|
2018-01-24 09:46:39 +00:00
|
|
|
this._auth._setUser(user);
|
|
|
|
});
|
2017-03-13 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 12:35:03 +00:00
|
|
|
/**
|
2017-11-17 14:22:46 +00:00
|
|
|
* Send verification email to current user.
|
2017-03-16 12:35:03 +00:00
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
sendEmailVerification(
|
|
|
|
actionCodeSettings?: ActionCodeSettings
|
|
|
|
): Promise<void> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
|
|
|
.sendEmailVerification(actionCodeSettings)
|
2018-01-25 18:25:39 +00:00
|
|
|
.then(user => {
|
2018-01-24 09:46:39 +00:00
|
|
|
this._auth._setUser(user);
|
|
|
|
});
|
2017-05-25 11:44:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
toJSON(): Object {
|
|
|
|
return Object.assign({}, this._user);
|
2017-03-16 12:35:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:13:17 +00:00
|
|
|
/**
|
|
|
|
*
|
2017-11-17 14:22:46 +00:00
|
|
|
* @param providerId
|
|
|
|
* @return {Promise.<TResult>|*}
|
2017-05-12 12:13:17 +00:00
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
unlink(providerId: string): Promise<User> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
|
|
|
.unlink(providerId)
|
|
|
|
.then(user => this._auth._setUser(user));
|
2017-05-12 12:13:17 +00:00
|
|
|
}
|
2017-03-13 20:27:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the current user's email
|
2017-03-18 01:10:34 +00:00
|
|
|
*
|
2017-03-13 20:27:44 +00:00
|
|
|
* @param {string} email The user's _new_ email
|
|
|
|
* @return {Promise} A promise resolved upon completion
|
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
updateEmail(email: string): Promise<void> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
|
|
|
.updateEmail(email)
|
2018-01-25 18:25:39 +00:00
|
|
|
.then(user => {
|
2018-01-24 09:46:39 +00:00
|
|
|
this._auth._setUser(user);
|
|
|
|
});
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
2017-11-17 14:22:46 +00:00
|
|
|
* Update the current user's password
|
|
|
|
* @param {string} password the new password
|
2017-03-13 20:27:44 +00:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
updatePassword(password: string): Promise<void> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
|
|
|
.updatePassword(password)
|
2018-01-25 18:25:39 +00:00
|
|
|
.then(user => {
|
2018-01-24 09:46:39 +00:00
|
|
|
this._auth._setUser(user);
|
|
|
|
});
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
2017-11-17 14:22:46 +00:00
|
|
|
* Update the current user's profile
|
|
|
|
* @param {Object} updates An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
|
2017-03-13 20:27:44 +00:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2018-01-18 09:49:11 +00:00
|
|
|
updateProfile(updates: UpdateProfile = {}): Promise<void> {
|
2018-01-24 09:46:39 +00:00
|
|
|
return getNativeModule(this._auth)
|
|
|
|
.updateProfile(updates)
|
2018-01-25 18:25:39 +00:00
|
|
|
.then(user => {
|
2018-01-24 09:46:39 +00:00
|
|
|
this._auth._setUser(user);
|
|
|
|
});
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 15:14:35 +00:00
|
|
|
/**
|
|
|
|
* KNOWN UNSUPPORTED METHODS
|
|
|
|
*/
|
|
|
|
|
|
|
|
linkWithPhoneNumber() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD(
|
|
|
|
'User',
|
|
|
|
'linkWithPhoneNumber'
|
|
|
|
)
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
linkWithPopup() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'linkWithPopup')
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
linkWithRedirect() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD(
|
|
|
|
'User',
|
|
|
|
'linkWithRedirect'
|
|
|
|
)
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reauthenticateWithPhoneNumber() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD(
|
|
|
|
'User',
|
|
|
|
'reauthenticateWithPhoneNumber'
|
|
|
|
)
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reauthenticateWithPopup() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD(
|
|
|
|
'User',
|
|
|
|
'reauthenticateWithPopup'
|
|
|
|
)
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reauthenticateWithRedirect() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD(
|
|
|
|
'User',
|
|
|
|
'reauthenticateWithRedirect'
|
|
|
|
)
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updatePhoneNumber() {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD(
|
|
|
|
'User',
|
|
|
|
'updatePhoneNumber'
|
|
|
|
)
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get refreshToken(): string {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_PROPERTY('User', 'refreshToken')
|
|
|
|
);
|
2017-09-23 15:14:35 +00:00
|
|
|
}
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|