Skip to content

Commit 805575a

Browse files
committed
feat(oauth): added signIn and link for oauth
1 parent 90a2589 commit 805575a

File tree

5 files changed

+93
-24
lines changed

5 files changed

+93
-24
lines changed

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

+42-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import com.facebook.react.bridge.WritableArray;
3131
import com.facebook.react.bridge.WritableMap;
3232
import com.google.android.gms.tasks.OnCompleteListener;
33+
import com.google.android.gms.tasks.OnFailureListener;
34+
import com.google.android.gms.tasks.OnSuccessListener;
35+
import com.google.android.gms.tasks.Task;
3336
import com.google.firebase.FirebaseApp;
3437
import com.google.firebase.FirebaseException;
3538
import com.google.firebase.FirebaseNetworkException;
@@ -203,7 +206,6 @@ public void addIdTokenListener(final String appName) {
203206

204207
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
205208
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
206-
207209
if (!mIdTokenListeners.containsKey(appName)) {
208210
FirebaseAuth.IdTokenListener newIdTokenListener =
209211
firebaseAuth1 -> {
@@ -838,6 +840,45 @@ private void signInWithCredential(
838840
});
839841
}
840842
}
843+
@ReactMethod
844+
public void signInWithProvider(String appName, String providerId, @Nullable String email, Promise promise){
845+
OAuthProvider.Builder provider = OAuthProvider.newBuilder(providerId);
846+
if(email != null){
847+
provider.addCustomParameter("login_hint", email);
848+
}
849+
Activity activity = getCurrentActivity();
850+
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
851+
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
852+
853+
OnSuccessListener onSuccess = new OnSuccessListener<AuthResult>(){
854+
@Override
855+
public void onSuccess(AuthResult authResult) {
856+
Log.d(TAG, "signInWithProvider:onComplete:success");
857+
promiseWithAuthResult(authResult, promise);
858+
}
859+
};
860+
861+
OnFailureListener onFailure = new OnFailureListener(){
862+
@Override
863+
public void onFailure(@NonNull Exception e) {
864+
Log.w(TAG, "signInWithProvider:onComplete:failure", e);
865+
promiseRejectAuthException(promise, e);
866+
}
867+
};
868+
869+
870+
Task<AuthResult> pendingResultTask = firebaseAuth.getPendingAuthResult();
871+
if(pendingResultTask != null){
872+
pendingResultTask
873+
.addOnSuccessListener(onSuccess)
874+
.addOnFailureListener(onFailure);
875+
} else {
876+
firebaseAuth
877+
.startActivityForSignInWithProvider(activity, provider.build())
878+
.addOnSuccessListener(onSuccess)
879+
.addOnFailureListener(onFailure);
880+
}
881+
}
841882

842883
/**
843884
* signInWithPhoneNumber

packages/auth/e2e/auth.e2e.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -912,12 +912,11 @@ describe('auth()', function () {
912912
});
913913

914914
describe('signInWithPopup', function () {
915-
it('should throw an unsupported error', function () {
916-
(() => {
917-
firebase.auth().signInWithPopup();
918-
}).should.throw(
919-
'firebase.auth().signInWithPopup() is unsupported by the native Firebase SDKs.',
920-
);
915+
it('should trigger the oauth flow', async function () {
916+
await (async () => {
917+
const provider = new firebase.auth.OAuthProvider('oidc.react.com');
918+
await firebase.auth().signInWithPopup(provider);
919+
}).should.not.throw();
921920
});
922921
});
923922

@@ -1025,12 +1024,11 @@ describe('auth()', function () {
10251024
});
10261025

10271026
describe('signInWithRedirect()', function () {
1028-
it('should throw an unsupported error', function () {
1029-
(() => {
1030-
firebase.auth().signInWithRedirect();
1031-
}).should.throw(
1032-
'firebase.auth().signInWithRedirect() is unsupported by the native Firebase SDKs.',
1033-
);
1027+
it('should trigger the oauth flow', async function () {
1028+
await (async () => {
1029+
const provider = new firebase.auth.OAuthProvider('oidc.react.com');
1030+
await firebase.auth().signInWithRedirect(provider);
1031+
}).should.not.throw();
10341032
});
10351033
});
10361034

packages/auth/e2e/provider.e2e.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ describe('auth() -> Providers', function () {
149149
describe('OAuthProvider', function () {
150150
describe('constructor', function () {
151151
it('should throw an unsupported error', function () {
152-
(() => new firebase.auth.OAuthProvider()).should.throw(
153-
'`new OAuthProvider()` is not supported on the native Firebase SDKs.',
154-
);
152+
(() => new firebase.auth.OAuthProvider('oidc.react.com')).should.not.throw();
155153
});
156154
});
157155

packages/auth/lib/index.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,41 @@ class FirebaseAuthModule extends FirebaseModule {
360360
throw new Error('firebase.auth().setPersistence() is unsupported by the native Firebase SDKs.');
361361
}
362362

363-
signInWithPopup() {
364-
throw new Error(
365-
'firebase.auth().signInWithPopup() is unsupported by the native Firebase SDKs.',
366-
);
363+
signInWithPopup(provider) {
364+
return this.native
365+
.signInWithProvider(provider.providerId, provider.customParameters?.login_hint)
366+
.then(userCredential => this._setUserCredential(userCredential));
367367
}
368368

369369
signInWithRedirect() {
370-
throw new Error(
371-
'firebase.auth().signInWithRedirect() is unsupported by the native Firebase SDKs.',
370+
return this.native
371+
.signInWithProvider(provider.providerId, provider.customParameters?.login_hint)
372+
.then(userCredential => this._setUserCredential(userCredential));
373+
}
374+
375+
async linkWithPopup(provider) {
376+
const credentials = await this.native.linkWithProvider(
377+
provider.providerId,
378+
provider.customParameters?.login_hint,
379+
);
380+
381+
return this.native.linkWithCredential(
382+
provider.providerId,
383+
credentials.token,
384+
credentials.secret,
385+
);
386+
}
387+
388+
async linkWithRedirect(provider) {
389+
const credentials = await this.native.linkWithProvider(
390+
provider.providerId,
391+
provider.customParameters?.login_hint,
392+
);
393+
394+
return this.native.linkWithCredential(
395+
provider.providerId,
396+
credentials.token,
397+
credentials.secret,
372398
);
373399
}
374400

packages/auth/lib/providers/OAuthProvider.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818
const providerId = 'oauth';
1919

2020
export default class OAuthProvider {
21-
constructor() {
22-
throw new Error('`new OAuthProvider()` is not supported on the native Firebase SDKs.');
21+
constructor(providerId) {
22+
this.providerId = providerId;
23+
}
24+
25+
customParameters = {};
26+
27+
setCustomParameters(customParameters) {
28+
this.customParameters = customParameters;
2329
}
2430

2531
static get PROVIDER_ID() {

0 commit comments

Comments
 (0)