all files / firebase/modules/auth/ User.js

100% Statements 48/48
73.33% Branches 11/15
100% Functions 44/44
100% Lines 48/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                                                                294× 294×                     22×             22×                         12×       22×       18×                       34×     34×                                                                                                                                                                                                         28×                                                                                                                                                                                                                        
/**
 * @flow
 * User representation wrapper
 */
import INTERNALS from '../../utils/internals';
import { getNativeModule } from '../../utils/native';
 
import type Auth from './';
import type {
  ActionCodeSettings,
  AuthCredential,
  NativeUser,
  UserCredential,
  UserInfo,
  UserMetadata,
} from './types';
 
type UpdateProfile = {
  displayName?: string,
  photoURL?: string,
};
 
export default class User {
  _auth: Auth;
  _user: NativeUser;
 
  /**
   *
   * @param auth Instance of Authentication class
   * @param user user result object from native
   */
  constructor(auth: Auth, user: NativeUser) {
    this._auth = auth;
    this._user = user;
  }
 
  /**
   * PROPERTIES
   */
 
  get displayName(): ?string {
    return this._user.displayName || null;
  }
 
  get email(): ?string {
    return this._user.email || null;
  }
 
  get emailVerified(): boolean {
    return this._user.emailVerified || false;
  }
 
  get isAnonymous(): boolean {
    return this._user.isAnonymous || false;
  }
 
  get metadata(): UserMetadata {
    return this._user.metadata;
  }
 
  get phoneNumber(): ?string {
    return this._user.phoneNumber || null;
  }
 
  get photoURL(): ?string {
    return this._user.photoURL || null;
  }
 
  get providerData(): Array<UserInfo> {
    return this._user.providerData;
  }
 
  get providerId(): string {
    return this._user.providerId;
  }
 
  get uid(): string {
    return this._user.uid;
  }
 
  /**
   * METHODS
   */
 
  /**
   * Delete the current user
   * @return {Promise}
   */
  delete(): Promise<void> {
    return getNativeModule(this._auth)
      .delete()
      .then(() => {
        this._auth._setUser();
      });
  }
 
  /**
   * get the token of current user
   * @return {Promise}
   */
  getIdToken(forceRefresh: boolean = false): Promise<string> {
    return getNativeModule(this._auth).getToken(forceRefresh);
  }
 
  /**
   * get the token of current user
   * @deprecated Deprecated getToken in favor of getIdToken.
   * @return {Promise}
   */
  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);
  }
 
  /**
   * @deprecated Deprecated linkWithCredential in favor of linkAndRetrieveDataWithCredential.
   * @param credential
   */
  linkWithCredential(credential: AuthCredential): Promise<User> {
    console.warn(
      'Deprecated firebase.User.prototype.linkWithCredential in favor of firebase.User.prototype.linkAndRetrieveDataWithCredential.'
    );
    return getNativeModule(this._auth)
      .linkWithCredential(
        credential.providerId,
        credential.token,
        credential.secret
      )
      .then(user => this._auth._setUser(user));
  }
 
  /**
   *
   * @param credential
   */
  linkAndRetrieveDataWithCredential(
    credential: AuthCredential
  ): Promise<UserCredential> {
    return getNativeModule(this._auth)
      .linkAndRetrieveDataWithCredential(
        credential.providerId,
        credential.token,
        credential.secret
      )
      .then(userCredential => this._auth._setUserCredential(userCredential));
  }
 
  /**
   * Re-authenticate a user with a third-party authentication provider
   * @return {Promise}         A promise resolved upon completion
   */
  reauthenticateWithCredential(credential: AuthCredential): Promise<void> {
    console.warn(
      'Deprecated firebase.User.prototype.reauthenticateWithCredential in favor of firebase.User.prototype.reauthenticateAndRetrieveDataWithCredential.'
    );
    return getNativeModule(this._auth)
      .reauthenticateWithCredential(
        credential.providerId,
        credential.token,
        credential.secret
      )
      .then(user => {
        this._auth._setUser(user);
      });
  }
 
  /**
   * Re-authenticate a user with a third-party authentication provider
   * @return {Promise}         A promise resolved upon completion
   */
  reauthenticateAndRetrieveDataWithCredential(
    credential: AuthCredential
  ): Promise<UserCredential> {
    return getNativeModule(this._auth)
      .reauthenticateAndRetrieveDataWithCredential(
        credential.providerId,
        credential.token,
        credential.secret
      )
      .then(userCredential => this._auth._setUserCredential(userCredential));
  }
 
  /**
   * Reload the current user
   * @return {Promise}
   */
  reload(): Promise<void> {
    return getNativeModule(this._auth)
      .reload()
      .then(user => {
        this._auth._setUser(user);
      });
  }
 
  /**
   * Send verification email to current user.
   */
  sendEmailVerification(
    actionCodeSettings?: ActionCodeSettings
  ): Promise<void> {
    return getNativeModule(this._auth)
      .sendEmailVerification(actionCodeSettings)
      .then(user => {
        this._auth._setUser(user);
      });
  }
 
  toJSON(): Object {
    return Object.assign({}, this._user);
  }
 
  /**
   *
   * @param providerId
   * @return {Promise.<TResult>|*}
   */
  unlink(providerId: string): Promise<User> {
    return getNativeModule(this._auth)
      .unlink(providerId)
      .then(user => this._auth._setUser(user));
  }
 
  /**
   * Update the current user's email
   *
   * @param  {string} email The user's _new_ email
   * @return {Promise}       A promise resolved upon completion
   */
  updateEmail(email: string): Promise<void> {
    return getNativeModule(this._auth)
      .updateEmail(email)
      .then(user => {
        this._auth._setUser(user);
      });
  }
 
  /**
   * Update the current user's password
   * @param  {string} password the new password
   * @return {Promise}
   */
  updatePassword(password: string): Promise<void> {
    return getNativeModule(this._auth)
      .updatePassword(password)
      .then(user => {
        this._auth._setUser(user);
      });
  }
 
  /**
   * 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: UpdateProfile = {}): Promise<void> {
    return getNativeModule(this._auth)
      .updateProfile(updates)
      .then(user => {
        this._auth._setUser(user);
      });
  }
 
  /**
   * KNOWN UNSUPPORTED METHODS
   */
 
  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(): string {
    throw new Error(
      INTERNALS.STRINGS.ERROR_UNSUPPORTED_CLASS_PROPERTY('User', 'refreshToken')
    );
  }
}