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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | 2× 2× 2× 4× 4× 4× 4× 4× 170× 170× 4× 4× 182× 182× 4× 4× 466× 466× 466× 466× 80× 80× 80× 80× 80× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 52× 50× 2× 2× 2× 52× 52× 14× 14× 8× 22× 16× 8× 8× 2× 10× 4× 8× 8× 2× 8× 2× 2× 6× 6× 6× 120× 4× 2× 2× 2× 2× 2× 2× | /** * @flow * Auth representation wrapper */ import User from './User'; import ModuleBase from '../../utils/ModuleBase'; import { getAppEventName, SharedEventEmitter } from '../../utils/events'; import { getLogger } from '../../utils/log'; import { getNativeModule } from '../../utils/native'; import INTERNALS from '../../utils/internals'; import ConfirmationResult from './ConfirmationResult'; // providers import EmailAuthProvider from './providers/EmailAuthProvider'; import PhoneAuthProvider from './providers/PhoneAuthProvider'; import GoogleAuthProvider from './providers/GoogleAuthProvider'; import GithubAuthProvider from './providers/GithubAuthProvider'; import OAuthProvider from './providers/OAuthProvider'; import TwitterAuthProvider from './providers/TwitterAuthProvider'; import FacebookAuthProvider from './providers/FacebookAuthProvider'; import PhoneAuthListener from './PhoneAuthListener'; import type { ActionCodeInfo, ActionCodeSettings, AuthCredential, NativeUser, NativeUserCredential, UserCredential, } from './types'; import type App from '../core/app'; type AuthState = { user?: NativeUser, }; const NATIVE_EVENTS = [ 'auth_state_changed', 'auth_id_token_changed', 'phone_auth_state_changed', ]; export const MODULE_NAME = 'RNFirebaseAuth'; export const NAMESPACE = 'auth'; export default class Auth extends ModuleBase { _authResult: boolean; _languageCode: string; _user: User | null; constructor(app: App) { super(app, { events: NATIVE_EVENTS, moduleName: MODULE_NAME, multiApp: true, namespace: NAMESPACE, }); this._user = null; this._authResult = false; this._languageCode = getNativeModule(this).APP_LANGUAGE[app._name] || getNativeModule(this).APP_LANGUAGE['[DEFAULT]']; SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public event name: onAuthStateChanged getAppEventName(this, 'auth_state_changed'), (state: AuthState) => { this._setUser(state.user); SharedEventEmitter.emit( getAppEventName(this, 'onAuthStateChanged'), this._user ); } ); SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public events based on event.type getAppEventName(this, 'phone_auth_state_changed'), (event: Object) => { const eventKey = `phone:auth:${event.requestKey}:${event.type}`; SharedEventEmitter.emit(eventKey, event.state); } ); SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public event name: onIdTokenChanged getAppEventName(this, 'auth_id_token_changed'), (auth: AuthState) => { this._setUser(auth.user); SharedEventEmitter.emit( getAppEventName(this, 'onIdTokenChanged'), this._user ); } ); getNativeModule(this).addAuthStateListener(); getNativeModule(this).addIdTokenListener(); } _setUser(user: ?NativeUser): ?User { this._authResult = true; this._user = user ? new User(this, user) : null; SharedEventEmitter.emit(getAppEventName(this, 'onUserChanged'), this._user); return this._user; } _setUserCredential(userCredential: NativeUserCredential): UserCredential { const user = new User(this, userCredential.user); this._authResult = true; this._user = user; SharedEventEmitter.emit(getAppEventName(this, 'onUserChanged'), this._user); return { additionalUserInfo: userCredential.additionalUserInfo, user, }; } /* * WEB API */ /** * Listen for auth changes. * @param listener */ onAuthStateChanged(listener: Function) { getLogger(this).info('Creating onAuthStateChanged listener'); SharedEventEmitter.addListener( getAppEventName(this, 'onAuthStateChanged'), listener ); Eif (this._authResult) listener(this._user || null); return () => { getLogger(this).info('Removing onAuthStateChanged listener'); SharedEventEmitter.removeListener( getAppEventName(this, 'onAuthStateChanged'), listener ); }; } /** * Listen for id token changes. * @param listener */ onIdTokenChanged(listener: Function) { getLogger(this).info('Creating onIdTokenChanged listener'); SharedEventEmitter.addListener( getAppEventName(this, 'onIdTokenChanged'), listener ); Eif (this._authResult) listener(this._user || null); return () => { getLogger(this).info('Removing onIdTokenChanged listener'); SharedEventEmitter.removeListener( getAppEventName(this, 'onIdTokenChanged'), listener ); }; } /** * Listen for user changes. * @param listener */ onUserChanged(listener: Function) { getLogger(this).info('Creating onUserChanged listener'); SharedEventEmitter.addListener( getAppEventName(this, 'onUserChanged'), listener ); Eif (this._authResult) listener(this._user || null); return () => { getLogger(this).info('Removing onUserChanged listener'); SharedEventEmitter.removeListener( getAppEventName(this, 'onUserChanged'), listener ); }; } /** * Sign the current user out * @return {Promise} */ signOut(): Promise<void> { return getNativeModule(this) .signOut() .then(() => { this._setUser(); }); } /** * Sign a user in anonymously * @deprecated Deprecated signInAnonymously in favor of signInAnonymouslyAndRetrieveData. * @return {Promise} A promise resolved upon completion */ signInAnonymously(): Promise<User> { console.warn( 'Deprecated firebase.User.prototype.signInAnonymously in favor of firebase.User.prototype.signInAnonymouslyAndRetrieveData.' ); return getNativeModule(this) .signInAnonymously() .then(user => this._setUser(user)); } /** * Sign a user in anonymously * @return {Promise} A promise resolved upon completion */ signInAnonymouslyAndRetrieveData(): Promise<UserCredential> { return getNativeModule(this) .signInAnonymouslyAndRetrieveData() .then(userCredential => this._setUserCredential(userCredential)); } /** * Create a user with the email/password functionality * @deprecated Deprecated createUserWithEmailAndPassword in favor of createUserAndRetrieveDataWithEmailAndPassword. * @param {string} email The user's email * @param {string} password The user's password * @return {Promise} A promise indicating the completion */ createUserWithEmailAndPassword( email: string, password: string ): Promise<User> { console.warn( 'Deprecated firebase.User.prototype.createUserWithEmailAndPassword in favor of firebase.User.prototype.createUserAndRetrieveDataWithEmailAndPassword.' ); return getNativeModule(this) .createUserWithEmailAndPassword(email, password) .then(user => this._setUser(user)); } /** * Create a user with the email/password functionality * @param {string} email The user's email * @param {string} password The user's password * @return {Promise} A promise indicating the completion */ createUserAndRetrieveDataWithEmailAndPassword( email: string, password: string ): Promise<UserCredential> { return getNativeModule(this) .createUserAndRetrieveDataWithEmailAndPassword(email, password) .then(userCredential => this._setUserCredential(userCredential)); } /** * Sign a user in with email/password * @deprecated Deprecated signInWithEmailAndPassword in favor of signInAndRetrieveDataWithEmailAndPassword * @param {string} email The user's email * @param {string} password The user's password * @return {Promise} A promise that is resolved upon completion */ signInWithEmailAndPassword(email: string, password: string): Promise<User> { console.warn( 'Deprecated firebase.User.prototype.signInWithEmailAndPassword in favor of firebase.User.prototype.signInAndRetrieveDataWithEmailAndPassword.' ); return getNativeModule(this) .signInWithEmailAndPassword(email, password) .then(user => this._setUser(user)); } /** * Sign a user in with email/password * @param {string} email The user's email * @param {string} password The user's password * @return {Promise} A promise that is resolved upon completion */ signInAndRetrieveDataWithEmailAndPassword( email: string, password: string ): Promise<UserCredential> { return getNativeModule(this) .signInAndRetrieveDataWithEmailAndPassword(email, password) .then(userCredential => this._setUserCredential(userCredential)); } /** * Sign the user in with a custom auth token * @deprecated Deprecated signInWithCustomToken in favor of signInAndRetrieveDataWithCustomToken * @param {string} customToken A self-signed custom auth token. * @return {Promise} A promise resolved upon completion */ signInWithCustomToken(customToken: string): Promise<User> { console.warn( 'Deprecated firebase.User.prototype.signInWithCustomToken in favor of firebase.User.prototype.signInAndRetrieveDataWithCustomToken.' ); return getNativeModule(this) .signInWithCustomToken(customToken) .then(user => this._setUser(user)); } /** * Sign the user in with a custom auth token * @param {string} customToken A self-signed custom auth token. * @return {Promise} A promise resolved upon completion */ signInAndRetrieveDataWithCustomToken( customToken: string ): Promise<UserCredential> { return getNativeModule(this) .signInAndRetrieveDataWithCustomToken(customToken) .then(userCredential => this._setUserCredential(userCredential)); } /** * Sign the user in with a third-party authentication provider * @deprecated Deprecated signInWithCredential in favor of signInAndRetrieveDataWithCredential. * @return {Promise} A promise resolved upon completion */ signInWithCredential(credential: AuthCredential): Promise<User> { console.warn( 'Deprecated firebase.User.prototype.signInWithCredential in favor of firebase.User.prototype.signInAndRetrieveDataWithCredential.' ); return getNativeModule(this) .signInWithCredential( credential.providerId, credential.token, credential.secret ) .then(user => this._setUser(user)); } /** * Sign the user in with a third-party authentication provider * @return {Promise} A promise resolved upon completion */ signInAndRetrieveDataWithCredential( credential: AuthCredential ): Promise<UserCredential> { return getNativeModule(this) .signInAndRetrieveDataWithCredential( credential.providerId, credential.token, credential.secret ) .then(userCredential => this._setUserCredential(userCredential)); } /** * Asynchronously signs in using a phone number. * */ signInWithPhoneNumber(phoneNumber: string): Promise<ConfirmationResult> { return getNativeModule(this) .signInWithPhoneNumber(phoneNumber) .then(result => new ConfirmationResult(this, result.verificationId)); } /** * Returns a PhoneAuthListener to listen to phone verification events, * on the final completion event a PhoneAuthCredential can be generated for * authentication purposes. * * @param phoneNumber * @param autoVerifyTimeout Android Only * @returns {PhoneAuthListener} */ verifyPhoneNumber( phoneNumber: string, autoVerifyTimeout?: number ): PhoneAuthListener { return new PhoneAuthListener(this, phoneNumber, autoVerifyTimeout); } /** * Send reset password instructions via email * @param {string} email The email to send password reset instructions */ sendPasswordResetEmail( email: string, actionCodeSettings?: ActionCodeSettings ): Promise<void> { return getNativeModule(this).sendPasswordResetEmail( email, actionCodeSettings ); } /** * Completes the password reset process, given a confirmation code and new password. * * @link https://firebase.google.com/docs/reference/js/firebase.auth.Auth#confirmPasswordReset * @param code * @param newPassword * @return {Promise.<Null>} */ confirmPasswordReset(code: string, newPassword: string): Promise<void> { return getNativeModule(this).confirmPasswordReset(code, newPassword); } /** * Applies a verification code sent to the user by email or other out-of-band mechanism. * * @link https://firebase.google.com/docs/reference/js/firebase.auth.Auth#applyActionCode * @param code * @return {Promise.<Null>} */ applyActionCode(code: string): Promise<void> { return getNativeModule(this).applyActionCode(code); } /** * Checks a verification code sent to the user by email or other out-of-band mechanism. * * @link https://firebase.google.com/docs/reference/js/firebase.auth.Auth#checkActionCode * @param code * @return {Promise.<any>|Promise<ActionCodeInfo>} */ checkActionCode(code: string): Promise<ActionCodeInfo> { return getNativeModule(this).checkActionCode(code); } /** * Returns a list of authentication providers that can be used to sign in a given user (identified by its main email address). * @return {Promise} */ fetchProvidersForEmail(email: string): Promise<string[]> { return getNativeModule(this).fetchProvidersForEmail(email); } verifyPasswordResetCode(code: string): Promise<string> { return getNativeModule(this).verifyPasswordResetCode(code); } /** * Sets the language for the auth module * @param code * @returns {*} */ set languageCode(code: string) { this._languageCode = code; getNativeModule(this).setLanguageCode(code); } /** * Get the currently signed in user * @return {Promise} */ get currentUser(): User | null { return this._user; } get languageCode(): string { return this._languageCode; } /** * KNOWN UNSUPPORTED METHODS */ getRedirectResult() { throw new Error( INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD( 'auth', 'getRedirectResult' ) ); } setPersistence() { throw new Error( INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD( 'auth', 'setPersistence' ) ); } signInWithPopup() { throw new Error( INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD( 'auth', 'signInWithPopup' ) ); } signInWithRedirect() { throw new Error( INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD( 'auth', 'signInWithRedirect' ) ); } // firebase issue - https://github.com/invertase/react-native-firebase/pull/655#issuecomment-349904680 useDeviceLanguage() { throw new Error( INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD( 'auth', 'useDeviceLanguage' ) ); } } export const statics = { EmailAuthProvider, PhoneAuthProvider, GoogleAuthProvider, GithubAuthProvider, TwitterAuthProvider, FacebookAuthProvider, OAuthProvider, PhoneAuthState: { CODE_SENT: 'sent', AUTO_VERIFY_TIMEOUT: 'timeout', AUTO_VERIFIED: 'verified', ERROR: 'error', }, }; |