From 1d0dffbefe3f9c885490a34c977e59b32b249790 Mon Sep 17 00:00:00 2001 From: Salakar Date: Mon, 25 Sep 2017 23:07:27 +0100 Subject: [PATCH] [auth] convert native error object in phoneAuthListener to js error + misc flow typings/fixes --- lib/modules/auth/PhoneAuthListener.js | 37 +++++++++++++++------------ 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/modules/auth/PhoneAuthListener.js b/lib/modules/auth/PhoneAuthListener.js index aa6037cf..2673fddc 100644 --- a/lib/modules/auth/PhoneAuthListener.js +++ b/lib/modules/auth/PhoneAuthListener.js @@ -1,6 +1,6 @@ // @flow import INTERNALS from './../../internals'; -import { generatePushID, isFunction, isAndroid, isIOS, isString } from './../../utils'; +import { generatePushID, isFunction, isAndroid, isIOS, isString, nativeToJSError } from './../../utils'; type PhoneAuthSnapshot = { state: 'sent' | 'timeout' | 'verified' | 'error', @@ -17,15 +17,16 @@ type PhoneAuthError = { }; export default class PhoneAuthListener { + _auth: Object; - _reject: Function | null; - _resolve: Function | null; - _promise: Promise | null; - _credential: Object | null; _timeout: number; - _phoneAuthRequestKey: string; _publicEvents: Object; _internalEvents: Object; + _reject: Function | null; + _resolve: Function | null; + _credential: Object | null; + _promise: Promise<*> | null; + _phoneAuthRequestKey: string; /** * @@ -33,7 +34,7 @@ export default class PhoneAuthListener { * @param phoneNumber * @param timeout */ - constructor(auth: Object, phoneNumber: string, timeout): PhoneAuthListener { + constructor(auth: Object, phoneNumber: string, timeout?: number) { this._auth = auth; this._reject = null; this._resolve = null; @@ -110,8 +111,6 @@ export default class PhoneAuthListener { */ _emitToErrorCb(snapshot) { const error = snapshot.error; - error.verificationId = snapshot.verificationId; - if (this._reject) this._reject(error); this._auth.emit(this._publicEvents.error, error); } @@ -234,17 +233,19 @@ export default class PhoneAuthListener { /** * Internal verification failed event handler - * @param errObject + * @param state * @private */ - _verificationFailedHandler(errObject) { + _verificationFailedHandler(state) { const snapshot: PhoneAuthSnapshot = { - verificationId: errObject.verificationId, + verificationId: state.verificationId, code: null, error: null, state: 'error', }; + const { code, message, nativeErrorMessage } = state.error; + snapshot.error = nativeToJSError(code, message, { nativeErrorMessage }); this._emitToObservers(snapshot); this._emitToErrorCb(snapshot); @@ -256,7 +257,7 @@ export default class PhoneAuthListener { -- PUBLIC API --------------*/ - on(event: string, observer: () => PhoneAuthSnapshot, errorCb?: () => PhoneAuthError, successCb?: () => PhoneAuthSnapshot) { + on(event: string, observer: () => PhoneAuthSnapshot, errorCb?: () => PhoneAuthError, successCb?: () => PhoneAuthSnapshot): this { if (!isString(event)) { throw new Error(INTERNALS.STRINGS.ERROR_MISSING_ARG_NAMED('event', 'string', 'on')); } @@ -286,17 +287,19 @@ export default class PhoneAuthListener { * Promise .then proxy * @param fn */ - then(fn) { + then(fn: () => PhoneAuthSnapshot) { this._promiseDeferred(); - return this._promise.then.bind(this._promise)(fn); + if (this._promise) return this._promise.then.bind(this._promise)(fn); + return undefined; // will never get here - just to keep flow happy } /** * Promise .catch proxy * @param fn */ - catch(fn) { + catch(fn: () => Error) { this._promiseDeferred(); - return this._promise.catch.bind(this._promise)(fn); + if (this._promise) return this._promise.catch.bind(this._promise)(fn); + return undefined; // will never get here - just to keep flow happy } }