2017-09-23 15:14:35 +00:00
|
|
|
import INTERNALS from './../../internals';
|
|
|
|
|
2017-03-02 13:09:41 +00:00
|
|
|
/**
|
|
|
|
* @url https://firebase.google.com/docs/reference/js/firebase.User
|
|
|
|
*/
|
2017-02-14 11:41:27 +00:00
|
|
|
export default class User {
|
2017-03-02 13:09:41 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param authClass 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-10-27 10:00:06 +00:00
|
|
|
constructor(authClass, userObj) {
|
2017-02-14 11:41:27 +00:00
|
|
|
this._auth = authClass;
|
2017-10-27 10:00:06 +00:00
|
|
|
this._user = userObj;
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNALS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a user property or null if does not exist
|
|
|
|
* @param prop
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_valueOrNull(prop) {
|
|
|
|
if (!Object.hasOwnProperty.call(this._user, prop)) return null;
|
|
|
|
return this._user[prop];
|
|
|
|
}
|
|
|
|
|
2017-03-14 19:04:16 +00:00
|
|
|
/**
|
|
|
|
* Returns a user property or false if does not exist
|
|
|
|
* @param prop
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_valueOrFalse(prop) {
|
|
|
|
if (!Object.hasOwnProperty.call(this._user, prop)) return false;
|
|
|
|
return this._user[prop];
|
|
|
|
}
|
|
|
|
|
2017-02-14 11:41:27 +00:00
|
|
|
/**
|
|
|
|
* PROPERTIES
|
|
|
|
*/
|
|
|
|
|
2017-07-12 15:26:02 +00:00
|
|
|
get displayName(): String | null {
|
2017-02-14 11:41:27 +00:00
|
|
|
return this._valueOrNull('displayName');
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:26:02 +00:00
|
|
|
get email(): String | null {
|
2017-02-14 11:41:27 +00:00
|
|
|
return this._valueOrNull('email');
|
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
get emailVerified(): Boolean {
|
2017-02-14 11:41:27 +00:00
|
|
|
return this._valueOrNull('emailVerified');
|
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
get isAnonymous(): Boolean {
|
2017-03-14 19:04:16 +00:00
|
|
|
return this._valueOrFalse('isAnonymous');
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 15:26:02 +00:00
|
|
|
get photoURL(): String | null {
|
2017-02-14 11:41:27 +00:00
|
|
|
return this._valueOrNull('photoURL');
|
|
|
|
}
|
|
|
|
|
|
|
|
get providerId() {
|
|
|
|
return this._valueOrNull('providerId');
|
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
get uid(): String {
|
2017-02-14 11:41:27 +00:00
|
|
|
return this._valueOrNull('uid');
|
|
|
|
}
|
|
|
|
|
|
|
|
// noinspection ReservedWordAsName
|
|
|
|
/**
|
|
|
|
* METHODS
|
|
|
|
*/
|
|
|
|
|
2017-03-13 20:01:45 +00:00
|
|
|
toJSON() {
|
2017-03-14 19:04:16 +00:00
|
|
|
return Object.assign({}, this._user);
|
2017-03-13 20:01:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
|
|
|
* Delete the current user
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
delete(): Promise<Object> {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.delete());
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 12:19:34 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param credential
|
|
|
|
*/
|
2017-05-25 11:44:39 +00:00
|
|
|
linkWithCredential(credential: CredentialType) {
|
2017-09-21 16:42:09 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.link(credential.providerId, credential.token, credential.secret));
|
2017-03-16 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 15:26:02 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param providerId
|
|
|
|
* @return {Promise.<TResult>|*}
|
|
|
|
*/
|
|
|
|
unlink(providerId: string) {
|
2017-09-23 21:34:40 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.unlink(providerId));
|
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-05-25 11:44:39 +00:00
|
|
|
reauthenticateWithCredential(credential: CredentialType) {
|
2017-09-21 16:42:09 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.reauthenticate(credential.providerId, credential.token, credential.secret));
|
2017-03-18 00:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
|
|
|
* Reload the current user
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
reload(): Promise<Object> {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.reload());
|
2017-03-13 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 12:35:03 +00:00
|
|
|
/**
|
|
|
|
* get the token of current user
|
2017-05-25 11:44:39 +00:00
|
|
|
* @deprecated Deprecated getToken in favor of getIdToken.
|
2017-03-16 12:35:03 +00:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-03-30 09:55:16 +00:00
|
|
|
getToken(forceRefresh: Boolean = false): Promise<Object> {
|
2017-05-25 11:44:39 +00:00
|
|
|
console.warn('Deprecated firebase.User.prototype.getToken in favor of firebase.User.prototype.getIdToken.');
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._native.getToken(forceRefresh);
|
2017-05-25 11:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the token of current user
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
getIdToken(forceRefresh: Boolean = false): Promise<Object> {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._native.getToken(forceRefresh);
|
2017-03-16 12:35:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:13:17 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
get providerData(): Array {
|
|
|
|
return this._valueOrNull('providerData') || [];
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
updateEmail(email: string): Promise<Object> {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.updateEmail(email));
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +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)
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
updateProfile(updates: Object = {}): Promise<Object> {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.updateProfile(updates));
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
|
|
|
* Update the current user's password
|
|
|
|
* @param {string} password the new password
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
updatePassword(password: string): Promise<Object> {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.updatePassword(password));
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:44 +00:00
|
|
|
/**
|
|
|
|
* Send verification email to current user.
|
|
|
|
*/
|
|
|
|
sendEmailVerification(): Promise<Object> {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._auth._interceptUserValue(this._auth._native.sendEmailVerification());
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|
2017-09-23 15:14:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* KNOWN UNSUPPORTED METHODS
|
|
|
|
*/
|
|
|
|
|
|
|
|
linkAndRetrieveDataWithCredential() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'linkAndRetrieveDataWithCredential'));
|
|
|
|
}
|
|
|
|
|
|
|
|
linkWithPhoneNumber() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'linkWithPhoneNumber'));
|
|
|
|
}
|
|
|
|
|
|
|
|
linkWithPopup() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'linkWithPopup'));
|
|
|
|
}
|
|
|
|
|
|
|
|
linkWithRedirect() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'linkWithRedirect'));
|
|
|
|
}
|
|
|
|
|
|
|
|
reauthenticateWithPhoneNumber() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'reauthenticateWithPhoneNumber'));
|
|
|
|
}
|
|
|
|
|
|
|
|
reauthenticateWithPopup() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'reauthenticateWithPopup'));
|
|
|
|
}
|
|
|
|
|
|
|
|
reauthenticateWithRedirect() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'reauthenticateWithRedirect'));
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePhoneNumber() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_METHOD('User', 'updatePhoneNumber'));
|
|
|
|
}
|
|
|
|
|
|
|
|
get refreshToken() {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_PROPERTY('User', 'refreshToken'));
|
|
|
|
}
|
2017-02-14 11:41:27 +00:00
|
|
|
}
|