Skip to content

Commit 90b6df6

Browse files
author
Armando Magalhaes
committed
feat: signinWithPhoneNumber. Related #8.
1 parent a76d326 commit 90b6df6

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/index.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type WrappedComponentProps = {
1010
signInWithFacebook: () => void;
1111
signInWithGithub: () => void;
1212
signInWithTwitter: () => void;
13+
signInWithPhoneNumber: (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => Promise<any>;
1314
signInAnonymously: () => void;
1415
signOut: () => void;
1516
setError: (error: any) => void;
@@ -68,16 +69,17 @@ const withFirebaseAuth = ({
6869

6970
setError = (error: any) => this.setState({ error });
7071

71-
tryTo = async (operation: () => any) => {
72+
async tryTo <T> (operation: () => Promise<T>): Promise<T> {
7273
try {
73-
return await operation();
74+
return operation();
7475
} catch(error) {
7576
this.setError(error.message);
77+
return error;
7678
}
7779
};
7880

79-
tryToSignInWithProvider = (provider: PossibleProviders) =>
80-
this.tryTo(() => {
81+
tryToSignInWithProvider = (provider: PossibleProviders): Promise<firebase.auth.UserCredential> =>
82+
this.tryTo<firebase.auth.UserCredential>(() => {
8183
const providerInstance = providers[provider];
8284

8385
if (!providerInstance) {
@@ -88,10 +90,10 @@ const withFirebaseAuth = ({
8890
});
8991

9092
signOut = () =>
91-
this.tryTo(() => firebaseAppAuth.signOut());
93+
this.tryTo<void>(() => firebaseAppAuth.signOut());
9294

9395
signInAnonymously = () =>
94-
this.tryTo(() => firebaseAppAuth.signInAnonymously());
96+
this.tryTo<firebase.auth.UserCredential>(() => firebaseAppAuth.signInAnonymously());
9597

9698
signInWithGithub = () =>
9799
this.tryToSignInWithProvider('githubProvider');
@@ -106,10 +108,13 @@ const withFirebaseAuth = ({
106108
this.tryToSignInWithProvider('facebookProvider');
107109

108110
signInWithEmailAndPassword = (email: string, password: string) =>
109-
this.tryTo(() => firebaseAppAuth.signInWithEmailAndPassword(email, password));
111+
this.tryTo<firebase.auth.UserCredential>(() => firebaseAppAuth.signInWithEmailAndPassword(email, password));
112+
113+
signInWithPhoneNumber = (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) =>
114+
this.tryTo<firebase.auth.ConfirmationResult>(() => firebaseAppAuth.signInWithPhoneNumber(phoneNumber, applicationVerifier))
110115

111116
createUserWithEmailAndPassword = (email: string, password: string) =>
112-
this.tryTo(() => firebaseAppAuth.createUserWithEmailAndPassword(email, password));
117+
this.tryTo<firebase.auth.UserCredential>(() => firebaseAppAuth.createUserWithEmailAndPassword(email, password));
113118

114119
sharedHandlers = {
115120
signInWithEmailAndPassword: this.signInWithEmailAndPassword,
@@ -118,6 +123,7 @@ const withFirebaseAuth = ({
118123
signInWithTwitter: this.signInWithTwitter,
119124
signInWithGoogle: this.signInWithGoogle,
120125
signInWithFacebook: this.signInWithFacebook,
126+
signInWithPhoneNumber: this.signInWithPhoneNumber,
121127
setError: this.setError,
122128
signInAnonymously: this.signInAnonymously,
123129
signOut: this.signOut,

src/test.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const fakeUser = {
1919
};
2020

2121
describe('withFirebaseAuth', () => {
22-
let currentAuthStateObserver = (_user: firebase.User) => {};
22+
let currentAuthStateObserver: any = (_user: firebase.User) => {};
2323
let unsubcribeAuthStateChangeMock = jest.fn();
2424

2525
beforeEach(() => {
@@ -30,6 +30,7 @@ describe('withFirebaseAuth', () => {
3030
testAppAuth.signInAnonymously = jest.fn();
3131
testAppAuth.signOut = jest.fn();
3232
testAppAuth.signInWithPopup = jest.fn();
33+
testAppAuth.signInWithPhoneNumber = jest.fn();
3334
testAppAuth.onAuthStateChanged = jest.fn(observer => {
3435
currentAuthStateObserver = observer;
3536
return unsubcribeAuthStateChangeMock;
@@ -204,8 +205,31 @@ describe('withFirebaseAuth', () => {
204205
});
205206

206207
it('should call createUserWithEmailAndPassword when prop is invoked', () => {
208+
const email = 'test';
209+
const password = 'test';
210+
207211
const WrappedComponent = ({ createUserWithEmailAndPassword }: WrappedComponentProps) =>
208-
<button onClick={() => createUserWithEmailAndPassword('test', 'test')}>createUserWithEmailAndPassword</button>;
212+
<button onClick={() => createUserWithEmailAndPassword(email, password)}>createUserWithEmailAndPassword</button>;
213+
214+
const EnhancedComponent = withFirebaseAuth({
215+
firebaseAppAuth: testAppAuth,
216+
})(WrappedComponent);
217+
218+
const wrapped = mount(<EnhancedComponent />);
219+
220+
wrapped.find('button').simulate('click');
221+
222+
expect(testAppAuth.createUserWithEmailAndPassword).toHaveBeenCalledWith(email, password);
223+
});
224+
225+
it('should call signInWithPhoneNumber when prop is invoked', () => {
226+
const phoneNumber = "666999666";
227+
const applicationVerifier = { type: '', verify: () => Promise.resolve('') };
228+
229+
const WrappedComponent = ({ signInWithPhoneNumber }: WrappedComponentProps) =>
230+
<button onClick={() => signInWithPhoneNumber(phoneNumber, applicationVerifier)}>
231+
signInWithPhoneNumber
232+
</button>;
209233

210234
const EnhancedComponent = withFirebaseAuth({
211235
firebaseAppAuth: testAppAuth,
@@ -215,7 +239,7 @@ describe('withFirebaseAuth', () => {
215239

216240
wrapped.find('button').simulate('click');
217241

218-
expect(testAppAuth.createUserWithEmailAndPassword).toHaveBeenCalled();
242+
expect(testAppAuth.signInWithPhoneNumber).toHaveBeenCalledWith(phoneNumber, applicationVerifier);
219243
});
220244

221245
it('should set an error when setError is invoked', () => {

0 commit comments

Comments
 (0)