react-native-firebase/lib/modules/auth/User.js

236 lines
6.0 KiB
JavaScript
Raw Normal View History

getNativeModule(this._auth)/**
2017-11-17 14:22:46 +00:00
* @flow
* User representation wrapper
2017-03-02 13:09:41 +00:00
*/
import INTERNALS from '../../utils/internals';
import { getNativeModule } from '../../utils/native';
2017-11-17 14:22:46 +00:00
import type Auth from './';
import type { ActionCodeSettings, AuthCredential } from '../../types';
2017-11-17 14:22:46 +00:00
type NativeUser = {
displayName?: string,
email?: string,
emailVerified?: boolean,
isAnonymous?: boolean,
phoneNumber?: string,
photoURL?: string,
providerData: UserInfo[],
providerId: string,
uid: string,
}
type UserInfo = {
displayName?: string,
email?: string,
phoneNumber?: string,
photoURL?: string,
providerId: string,
uid: string,
}
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
* @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-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
}
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-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
*/
/**
* Delete the current user
* @return {Promise}
*/
2017-11-17 14:22:46 +00:00
delete(): Promise<void> {
return this._auth
._interceptUndefinedUserValue(getNativeModule(this._auth).delete());
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> {
return getNativeModule(this._auth).getToken(forceRefresh);
2017-03-16 12:19:34 +00:00
}
/**
*
2017-11-17 14:22:46 +00:00
* @param credential
*/
2017-11-17 14:22:46 +00:00
linkWithCredential(credential: AuthCredential): Promise<User> {
return this._auth
._interceptUserValue(getNativeModule(this._auth).link(credential.providerId, credential.token, credential.secret));
}
/**
* 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> {
return this._auth
._interceptUndefinedUserValue(getNativeModule(this._auth).reauthenticate(credential.providerId, credential.token, credential.secret));
}
/**
* Reload the current user
* @return {Promise}
*/
2017-11-17 14:22:46 +00:00
reload(): Promise<void> {
return this._auth
._interceptUndefinedUserValue(getNativeModule(this._auth).reload());
}
/**
2017-11-17 14:22:46 +00:00
* Send verification email to current user.
*/
sendEmailVerification(actionCodeSettings?: ActionCodeSettings): Promise<void> {
2017-11-17 14:22:46 +00:00
return this._auth
._interceptUndefinedUserValue(getNativeModule(this._auth).sendEmailVerification(actionCodeSettings));
}
2017-11-17 14:22:46 +00:00
toJSON(): Object {
return Object.assign({}, this._user);
}
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> {
return this._auth._interceptUserValue(getNativeModule(this._auth).unlink(providerId));
2017-05-12 12:13:17 +00:00
}
/**
* Update the current user's email
*
* @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> {
return this._auth
._interceptUndefinedUserValue(getNativeModule(this._auth).updateEmail(email));
2017-02-14 11:41:27 +00:00
}
/**
2017-11-17 14:22:46 +00:00
* Update the current user's password
* @param {string} password the new password
* @return {Promise}
*/
2017-11-17 14:22:46 +00:00
updatePassword(password: string): Promise<void> {
return this._auth
._interceptUndefinedUserValue(getNativeModule(this._auth).updatePassword(password));
2017-02-14 11:41:27 +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)
* @return {Promise}
*/
2017-11-17 14:22:46 +00:00
updateProfile(updates: Object = {}): Promise<void> {
return this._auth
._interceptUndefinedUserValue(getNativeModule(this._auth).updateProfile(updates));
2017-02-14 11:41:27 +00:00
}
/**
2017-11-17 14:22:46 +00:00
* get the token of current user
* @deprecated Deprecated getToken in favor of getIdToken.
* @return {Promise}
*/
2017-11-17 14:22:46 +00:00
getToken(forceRefresh: boolean = false): Promise<Object> {
console.warn('Deprecated firebase.User.prototype.getToken in favor of firebase.User.prototype.getIdToken.');
return getNativeModule(this._auth).getToken(forceRefresh);
2017-02-14 11:41:27 +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'));
}
2017-11-17 14:22:46 +00:00
get refreshToken(): string {
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_PROPERTY('User', 'refreshToken'));
}
2017-02-14 11:41:27 +00:00
}