Skip to content

Commit 7c082be

Browse files
committed
fix(auth, phone): call verifyPhoneNumber callbacks correctly
success callback seems fine, error should be fine sometimes but is unstable on simulator and emulator during testing
1 parent 88a3aa9 commit 7c082be

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,12 @@ public void verifyPhoneNumber(
958958
final String requestKey,
959959
final int timeout,
960960
final boolean forceResend) {
961+
Log.d(TAG, "verifyPhoneNumber:" + phoneNumber);
962+
961963
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
962964
final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
963965
final Activity activity = getCurrentActivity();
964966

965-
Log.d(TAG, "verifyPhoneNumber:" + phoneNumber);
966-
967967
// reset force resending token if phone number changes
968968
if (!phoneNumber.equals(mLastPhoneNumber)) {
969969
mForceResendingToken = null;

packages/auth/e2e/phone.e2e.js

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,72 @@ describe('auth() => Phone', function () {
107107

108108
it('successfully runs verification complete handler', async function () {
109109
const testPhone = getRandomPhoneNumber();
110+
const thenCb = sinon.spy();
111+
await firebase.auth().verifyPhoneNumber(testPhone).then(thenCb);
112+
thenCb.should.be.calledOnce();
113+
const successAuthSnapshot = thenCb.args[0][0];
114+
if (device.getPlatform() === 'ios') {
115+
successAuthSnapshot.state.should.equal('sent');
116+
} else {
117+
successAuthSnapshot.state.should.equal('timeout');
118+
}
119+
});
120+
121+
it('successfully runs and calls success callback', async function () {
122+
const testPhone = getRandomPhoneNumber();
123+
const successCb = sinon.spy();
124+
const observerCb = sinon.spy();
125+
const errorCb = sinon.spy();
126+
110127
await firebase
111128
.auth()
112129
.verifyPhoneNumber(testPhone)
113-
.then($ => $);
114-
115-
return Promise.resolve();
130+
.on('state_changed', observerCb, errorCb, successCb);
131+
132+
await Utils.spyToBeCalledOnceAsync(successCb);
133+
errorCb.should.be.callCount(0);
134+
successCb.should.be.calledOnce();
135+
let observerAuthSnapshot = observerCb.args[0][0];
136+
const successAuthSnapshot = successCb.args[0][0];
137+
successAuthSnapshot.verificationId.should.be.a.String();
138+
if (device.getPlatform() === 'ios') {
139+
observerCb.should.be.calledOnce();
140+
successAuthSnapshot.state.should.equal('sent');
141+
} else {
142+
// android waits for SMS auto retrieval which does not work on an emulator
143+
// it gets a sent and a timeout message on observer, just the timeout on success
144+
observerCb.should.be.calledTwice();
145+
observerAuthSnapshot = observerCb.args[1][0];
146+
successAuthSnapshot.state.should.equal('timeout');
147+
}
148+
JSON.stringify(successAuthSnapshot).should.equal(JSON.stringify(observerAuthSnapshot));
116149
});
117150

118-
it('successfully runs and adds emitters', async function () {
119-
const testPhone = await getRandomPhoneNumber();
120-
const obervserCb = () => {};
121-
const errorCb = () => {};
122-
const successCb = () => {
123-
return Promise.resolve();
124-
};
151+
// TODO determine why this is not stable on the emulator, is it also not stable on real device?
152+
xit('successfully runs and calls error callback', async function () {
153+
const successCb = sinon.spy();
154+
const observerCb = sinon.spy();
155+
const errorCb = sinon.spy();
125156

126157
await firebase
127158
.auth()
128-
.verifyPhoneNumber(testPhone)
129-
.on('state_changed', obervserCb, errorCb, successCb, () => {});
159+
.verifyPhoneNumber('notaphonenumber')
160+
.on('state_changed', observerCb, errorCb, successCb);
161+
162+
await Utils.spyToBeCalledOnceAsync(errorCb);
163+
errorCb.should.be.calledOnce();
164+
observerCb.should.be.calledOnce();
165+
// const observerEvent = observerCb.args[0][0];
166+
successCb.should.be.callCount(0);
167+
// const errorEvent = errorCb.args[0][0];
168+
// errorEvent.error.should.containEql('auth/invalid-phone-number');
169+
// JSON.stringify(errorEvent).should.equal(JSON.stringify(observerEvent));
130170
});
131171

132172
it('catches an error and emits an error event', async function () {
133-
return firebase
134-
.auth()
135-
.verifyPhoneNumber('test')
136-
.catch(() => Promise.resolve());
173+
const catchCb = sinon.spy();
174+
await firebase.auth().verifyPhoneNumber('badphonenumber').catch(catchCb);
175+
catchCb.should.be.calledOnce();
137176
});
138177
});
139178
});

packages/auth/lib/PhoneAuthListener.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,16 @@ export default class PhoneAuthListener {
206206
this._addUserObserver(observer);
207207

208208
if (isFunction(errorCb)) {
209-
const subscription = this._auth.emitter.addListener(this._publicEvents.error, () => {
210-
errorCb;
209+
const subscription = this._auth.emitter.addListener(this._publicEvents.error, event => {
211210
subscription.remove();
211+
errorCb(event);
212212
});
213213
}
214214

215215
if (isFunction(successCb)) {
216-
const subscription = this._auth.emitter.addListener(this._publicEvents.success, () => {
217-
successCb;
216+
const subscription = this._auth.emitter.addListener(this._publicEvents.success, event => {
218217
subscription.remove();
218+
successCb(event);
219219
});
220220
}
221221

0 commit comments

Comments
 (0)