UNPKG

react-native-firebase-compiled

Version:

A well tested, feature rich Firebase implementation for React Native, supporting iOS & Android. Individual module support for Admob, Analytics, Auth, Crash Reporting, Cloud Firestore, Database, Dynamic Links, Functions, Messaging (FCM), Remote Config, Sto

505 lines (389 loc) 17.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.statics = exports.default = exports.NAMESPACE = exports.MODULE_NAME = void 0; var _User = _interopRequireDefault(require("./User")); var _ModuleBase = _interopRequireDefault(require("../../utils/ModuleBase")); var _events = require("../../utils/events"); var _utils = require("../../utils"); var _log = require("../../utils/log"); var _native = require("../../utils/native"); var _internals = _interopRequireDefault(require("../../utils/internals")); var _ConfirmationResult = _interopRequireDefault(require("./phone/ConfirmationResult")); var _PhoneAuthListener = _interopRequireDefault(require("./phone/PhoneAuthListener")); var _EmailAuthProvider = _interopRequireDefault(require("./providers/EmailAuthProvider")); var _PhoneAuthProvider = _interopRequireDefault(require("./providers/PhoneAuthProvider")); var _GoogleAuthProvider = _interopRequireDefault(require("./providers/GoogleAuthProvider")); var _GithubAuthProvider = _interopRequireDefault(require("./providers/GithubAuthProvider")); var _OAuthProvider = _interopRequireDefault(require("./providers/OAuthProvider")); var _TwitterAuthProvider = _interopRequireDefault(require("./providers/TwitterAuthProvider")); var _FacebookAuthProvider = _interopRequireDefault(require("./providers/FacebookAuthProvider")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const NATIVE_EVENTS = ['auth_state_changed', 'auth_id_token_changed', 'phone_auth_state_changed']; const MODULE_NAME = 'RNFirebaseAuth'; exports.MODULE_NAME = MODULE_NAME; const NAMESPACE = 'auth'; exports.NAMESPACE = NAMESPACE; class Auth extends _ModuleBase.default { constructor(app) { super(app, { events: NATIVE_EVENTS, moduleName: MODULE_NAME, hasMultiAppSupport: true, hasCustomUrlSupport: false, namespace: NAMESPACE }); _defineProperty(this, "_authResult", void 0); _defineProperty(this, "_languageCode", void 0); _defineProperty(this, "_user", void 0); this._user = null; this._authResult = false; this._languageCode = (0, _native.getNativeModule)(this).APP_LANGUAGE[app._name] || (0, _native.getNativeModule)(this).APP_LANGUAGE['[DEFAULT]']; _events.SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public event name: onAuthStateChanged (0, _events.getAppEventName)(this, 'auth_state_changed'), state => { this._setUser(state.user); _events.SharedEventEmitter.emit((0, _events.getAppEventName)(this, 'onAuthStateChanged'), this._user); }); _events.SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public events based on event.type (0, _events.getAppEventName)(this, 'phone_auth_state_changed'), event => { const eventKey = `phone:auth:${event.requestKey}:${event.type}`; _events.SharedEventEmitter.emit(eventKey, event.state); }); _events.SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public event name: onIdTokenChanged (0, _events.getAppEventName)(this, 'auth_id_token_changed'), auth => { this._setUser(auth.user); _events.SharedEventEmitter.emit((0, _events.getAppEventName)(this, 'onIdTokenChanged'), this._user); }); (0, _native.getNativeModule)(this).addAuthStateListener(); (0, _native.getNativeModule)(this).addIdTokenListener(); } _setUser(user) { this._user = user ? new _User.default(this, user) : null; this._authResult = true; _events.SharedEventEmitter.emit((0, _events.getAppEventName)(this, 'onUserChanged'), this._user); return this._user; } _setUserCredential(userCredential) { const user = new _User.default(this, userCredential.user); this._user = user; this._authResult = true; _events.SharedEventEmitter.emit((0, _events.getAppEventName)(this, 'onUserChanged'), this._user); return { additionalUserInfo: userCredential.additionalUserInfo, user }; } /* * WEB API */ /** * Listen for auth changes. * @param listener */ onAuthStateChanged(listener) { (0, _log.getLogger)(this).info('Creating onAuthStateChanged listener'); _events.SharedEventEmitter.addListener((0, _events.getAppEventName)(this, 'onAuthStateChanged'), listener); if (this._authResult) listener(this._user || null); return () => { (0, _log.getLogger)(this).info('Removing onAuthStateChanged listener'); _events.SharedEventEmitter.removeListener((0, _events.getAppEventName)(this, 'onAuthStateChanged'), listener); }; } /** * Listen for id token changes. * @param listener */ onIdTokenChanged(listener) { (0, _log.getLogger)(this).info('Creating onIdTokenChanged listener'); _events.SharedEventEmitter.addListener((0, _events.getAppEventName)(this, 'onIdTokenChanged'), listener); if (this._authResult) listener(this._user || null); return () => { (0, _log.getLogger)(this).info('Removing onIdTokenChanged listener'); _events.SharedEventEmitter.removeListener((0, _events.getAppEventName)(this, 'onIdTokenChanged'), listener); }; } /** * Listen for user changes. * @param listener */ onUserChanged(listener) { (0, _log.getLogger)(this).info('Creating onUserChanged listener'); _events.SharedEventEmitter.addListener((0, _events.getAppEventName)(this, 'onUserChanged'), listener); if (this._authResult) listener(this._user || null); return () => { (0, _log.getLogger)(this).info('Removing onUserChanged listener'); _events.SharedEventEmitter.removeListener((0, _events.getAppEventName)(this, 'onUserChanged'), listener); }; } /** * Sign the current user out * @return {Promise} */ signOut() { return (0, _native.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() { console.warn('Deprecated firebase.User.prototype.signInAnonymously in favor of firebase.User.prototype.signInAnonymouslyAndRetrieveData.'); return (0, _native.getNativeModule)(this).signInAnonymously().then(user => this._setUser(user)); } /** * Sign a user in anonymously * @return {Promise} A promise resolved upon completion */ signInAnonymouslyAndRetrieveData() { return (0, _native.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, password) { console.warn('Deprecated firebase.User.prototype.createUserWithEmailAndPassword in favor of firebase.User.prototype.createUserAndRetrieveDataWithEmailAndPassword.'); return (0, _native.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, password) { return (0, _native.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, password) { console.warn('Deprecated firebase.User.prototype.signInWithEmailAndPassword in favor of firebase.User.prototype.signInAndRetrieveDataWithEmailAndPassword.'); return (0, _native.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, password) { return (0, _native.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) { console.warn('Deprecated firebase.User.prototype.signInWithCustomToken in favor of firebase.User.prototype.signInAndRetrieveDataWithCustomToken.'); return (0, _native.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) { return (0, _native.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) { console.warn('Deprecated firebase.User.prototype.signInWithCredential in favor of firebase.User.prototype.signInAndRetrieveDataWithCredential.'); return (0, _native.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) { return (0, _native.getNativeModule)(this).signInAndRetrieveDataWithCredential(credential.providerId, credential.token, credential.secret).then(userCredential => this._setUserCredential(userCredential)); } /** * Asynchronously signs in using a phone number. * */ signInWithPhoneNumber(phoneNumber, forceResend) { if (_utils.isAndroid) { return (0, _native.getNativeModule)(this).signInWithPhoneNumber(phoneNumber, forceResend || false).then(result => new _ConfirmationResult.default(this, result.verificationId)); } return (0, _native.getNativeModule)(this).signInWithPhoneNumber(phoneNumber).then(result => new _ConfirmationResult.default(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 autoVerifyTimeoutOrForceResend Android Only * @param forceResend Android Only * @returns {PhoneAuthListener} */ verifyPhoneNumber(phoneNumber, autoVerifyTimeoutOrForceResend, forceResend) { let _forceResend = forceResend; let _autoVerifyTimeout = 60; if ((0, _utils.isBoolean)(autoVerifyTimeoutOrForceResend)) { _forceResend = autoVerifyTimeoutOrForceResend; } else { _autoVerifyTimeout = autoVerifyTimeoutOrForceResend; } return new _PhoneAuthListener.default(this, phoneNumber, _autoVerifyTimeout, _forceResend); } /** * Send reset password instructions via email * @param {string} email The email to send password reset instructions * @param actionCodeSettings */ sendPasswordResetEmail(email, actionCodeSettings) { return (0, _native.getNativeModule)(this).sendPasswordResetEmail(email, actionCodeSettings); } /** * Sends a sign-in email link to the user with the specified email * @param {string} email The email account to sign in with. * @param actionCodeSettings */ sendSignInLinkToEmail(email, actionCodeSettings) { return (0, _native.getNativeModule)(this).sendSignInLinkToEmail(email, actionCodeSettings); } /** * Checks if an incoming link is a sign-in with email link. * @param {string} emailLink Sign-in email link. */ isSignInWithEmailLink(emailLink) { return typeof emailLink === 'string' && (emailLink.includes('mode=signIn') || emailLink.includes('mode%3DsignIn')) && (emailLink.includes('oobCode=') || emailLink.includes('oobCode%3D')); } /** * Asynchronously signs in using an email and sign-in email link. * * @param {string} email The email account to sign in with. * @param {string} emailLink Sign-in email link. * @return {Promise} A promise resolved upon completion */ signInWithEmailLink(email, emailLink) { return (0, _native.getNativeModule)(this).signInWithEmailLink(email, emailLink).then(userCredential => this._setUserCredential(userCredential)); } /** * 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, newPassword) { return (0, _native.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) { return (0, _native.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) { return (0, _native.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} * @Deprecated */ fetchProvidersForEmail(email) { console.warn('Deprecated firebase.auth().fetchProvidersForEmail in favor of firebase.auth().fetchSignInMethodsForEmail()'); return (0, _native.getNativeModule)(this).fetchSignInMethodsForEmail(email); } /** * Returns a list of authentication methods that can be used to sign in a given user (identified by its main email address). * @return {Promise} */ fetchSignInMethodsForEmail(email) { return (0, _native.getNativeModule)(this).fetchSignInMethodsForEmail(email); } verifyPasswordResetCode(code) { return (0, _native.getNativeModule)(this).verifyPasswordResetCode(code); } /** * Sets the language for the auth module * @param code * @returns {*} */ set languageCode(code) { this._languageCode = code; (0, _native.getNativeModule)(this).setLanguageCode(code); } /** * Get the currently signed in user * @return {Promise} */ get currentUser() { return this._user; } get languageCode() { return this._languageCode; } /** * KNOWN UNSUPPORTED METHODS */ getRedirectResult() { throw new Error(_internals.default.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'getRedirectResult')); } setPersistence() { throw new Error(_internals.default.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'setPersistence')); } signInWithPopup() { throw new Error(_internals.default.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'signInWithPopup')); } signInWithRedirect() { throw new Error(_internals.default.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.default.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'useDeviceLanguage')); } } exports.default = Auth; const statics = { EmailAuthProvider: _EmailAuthProvider.default, PhoneAuthProvider: _PhoneAuthProvider.default, GoogleAuthProvider: _GoogleAuthProvider.default, GithubAuthProvider: _GithubAuthProvider.default, TwitterAuthProvider: _TwitterAuthProvider.default, FacebookAuthProvider: _FacebookAuthProvider.default, OAuthProvider: _OAuthProvider.default, PhoneAuthState: { CODE_SENT: 'sent', AUTO_VERIFY_TIMEOUT: 'timeout', AUTO_VERIFIED: 'verified', ERROR: 'error' } }; exports.statics = statics;