mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-23 20:10:05 +08:00
Merge commit '49d29b53f21e530f5c918e472db93ee856947426'
This commit is contained in:
@@ -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,14 +34,14 @@ 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;
|
||||
this._promise = null;
|
||||
this._credential = null;
|
||||
|
||||
this._timeout = timeout || 20000; // 20 secs
|
||||
this._timeout = timeout || 20; // 20 secs
|
||||
this._phoneAuthRequestKey = generatePushID();
|
||||
|
||||
// internal events
|
||||
@@ -65,11 +66,20 @@ export default class PhoneAuthListener {
|
||||
this._subscribeToEvents();
|
||||
|
||||
// start verification flow natively
|
||||
this._auth._native.verifyPhoneNumber(
|
||||
phoneNumber,
|
||||
this._phoneAuthRequestKey,
|
||||
this._timeout,
|
||||
);
|
||||
if (isAndroid) {
|
||||
this._auth._native.verifyPhoneNumber(
|
||||
phoneNumber,
|
||||
this._phoneAuthRequestKey,
|
||||
this._timeout,
|
||||
);
|
||||
}
|
||||
|
||||
if (isIOS) {
|
||||
this._auth._native.verifyPhoneNumber(
|
||||
phoneNumber,
|
||||
this._phoneAuthRequestKey,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,8 +120,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);
|
||||
}
|
||||
@@ -174,12 +182,12 @@ export default class PhoneAuthListener {
|
||||
|
||||
/**
|
||||
* Internal code sent event handler
|
||||
* @param verificationId
|
||||
* @private
|
||||
* @param credential
|
||||
*/
|
||||
_codeSentHandler(verificationId) {
|
||||
_codeSentHandler(credential) {
|
||||
const snapshot: PhoneAuthSnapshot = {
|
||||
verificationId,
|
||||
verificationId: credential.verificationId,
|
||||
code: null,
|
||||
error: null,
|
||||
state: 'sent',
|
||||
@@ -199,12 +207,12 @@ export default class PhoneAuthListener {
|
||||
|
||||
/**
|
||||
* Internal code auto retrieve timeout event handler
|
||||
* @param verificationId
|
||||
* @private
|
||||
* @param credential
|
||||
*/
|
||||
_codeAutoRetrievalTimeoutHandler(verificationId) {
|
||||
_codeAutoRetrievalTimeoutHandler(credential) {
|
||||
const snapshot: PhoneAuthSnapshot = {
|
||||
verificationId,
|
||||
verificationId: credential.verificationId,
|
||||
code: null,
|
||||
error: null,
|
||||
state: 'timeout',
|
||||
@@ -234,17 +242,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 +266,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 +296,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ export default class Auth extends ModuleBase {
|
||||
static _NATIVE_MODULE = 'RNFirebaseAuth';
|
||||
|
||||
_user: User | null;
|
||||
_native: Object;
|
||||
_getAppEventName: Function;
|
||||
_authResult: AuthResultType | null;
|
||||
authenticated: boolean;
|
||||
|
||||
@@ -35,6 +37,13 @@ export default class Auth extends ModuleBase {
|
||||
this._onAuthStateChanged.bind(this),
|
||||
);
|
||||
|
||||
this.addListener(
|
||||
// sub to internal native event - this fans out to
|
||||
// public events based on event.type
|
||||
this._getAppEventName('phone_auth_state_changed'),
|
||||
this._onPhoneAuthStateChanged.bind(this),
|
||||
);
|
||||
|
||||
this.addListener(
|
||||
// sub to internal native event - this fans out to
|
||||
// public event name: onIdTokenChanged
|
||||
@@ -46,6 +55,16 @@ export default class Auth extends ModuleBase {
|
||||
this._native.addIdTokenListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Route a phone state change event to the correct listeners
|
||||
* @param event
|
||||
* @private
|
||||
*/
|
||||
_onPhoneAuthStateChanged(event: Object) {
|
||||
const eventKey = `phone:auth:${event.requestKey}:${event.type}`;
|
||||
this.emit(eventKey, event.state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal auth changed listener
|
||||
* @param auth
|
||||
@@ -100,7 +119,7 @@ export default class Auth extends ModuleBase {
|
||||
* Intercept all user actions and send their results to
|
||||
* auth state change before resolving
|
||||
* @param promise
|
||||
* @returns {Promise.<TResult>|*}
|
||||
* @returns {Promise.<*>}
|
||||
* @private
|
||||
*/
|
||||
_interceptUserValue(promise) {
|
||||
@@ -234,7 +253,7 @@ export default class Auth extends ModuleBase {
|
||||
* @param newPassword
|
||||
* @return {Promise.<Null>}
|
||||
*/
|
||||
confirmPasswordReset(code: string, newPassword: string): Promise<Null> {
|
||||
confirmPasswordReset(code: string, newPassword: string): Promise<null> {
|
||||
return this._native.confirmPasswordReset(code, newPassword);
|
||||
}
|
||||
|
||||
@@ -245,7 +264,7 @@ export default class Auth extends ModuleBase {
|
||||
* @param code
|
||||
* @return {Promise.<Null>}
|
||||
*/
|
||||
applyActionCode(code: string): Promise<Any> {
|
||||
applyActionCode(code: string): Promise<any> {
|
||||
return this._native.applyActionCode(code);
|
||||
}
|
||||
|
||||
@@ -254,9 +273,9 @@ export default class Auth extends ModuleBase {
|
||||
*
|
||||
* @link https://firebase.google.com/docs/reference/js/firebase.auth.Auth#checkActionCode
|
||||
* @param code
|
||||
* @return {Promise.<Any>|Promise<ActionCodeInfo>}
|
||||
* @return {Promise.<any>|Promise<ActionCodeInfo>}
|
||||
*/
|
||||
checkActionCode(code: string): Promise<Any> {
|
||||
checkActionCode(code: string): Promise<any> {
|
||||
return this._native.checkActionCode(code);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Platform } from 'react-native';
|
||||
import { Platform, NativeModules } from 'react-native';
|
||||
import ModuleBase from './../../utils/ModuleBase';
|
||||
import RemoteMessage from './RemoteMessage';
|
||||
|
||||
@@ -25,6 +25,8 @@ const WILL_PRESENT_RESULT = {
|
||||
None: 'UNNotificationPresentationOptionNone',
|
||||
};
|
||||
|
||||
const FirebaseMessaging = NativeModules.FirebaseMessaging;
|
||||
|
||||
/**
|
||||
* IOS only finish function
|
||||
* @param data
|
||||
@@ -109,6 +111,14 @@ export default class Messaging extends ModuleBase {
|
||||
return this._native.getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset Instance ID and revokes all tokens.
|
||||
* @returns {*|Promise.<*>}
|
||||
*/
|
||||
deleteInstanceId() {
|
||||
return this._native.deleteInstanceId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and display a local notification
|
||||
* @param notification
|
||||
|
||||
@@ -6,6 +6,8 @@ export default class PerformanceMonitoring extends ModuleBase {
|
||||
static _NAMESPACE = 'perf';
|
||||
static _NATIVE_MODULE = 'RNFirebasePerformance';
|
||||
|
||||
_native: Object;
|
||||
|
||||
constructor(firebaseApp: Object, options: Object = {}) {
|
||||
super(firebaseApp, options);
|
||||
}
|
||||
@@ -26,8 +28,4 @@ export default class PerformanceMonitoring extends ModuleBase {
|
||||
newTrace(trace: string): void {
|
||||
return new Trace(this, trace);
|
||||
}
|
||||
|
||||
get namespace(): string {
|
||||
return 'firebase:perf';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user