Skip to content

Commit

Permalink
feat(oauth): added signIn and link for oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
Aetherall committed Apr 3, 2023
1 parent 90a2589 commit 805575a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseNetworkException;
Expand Down Expand Up @@ -203,7 +206,6 @@ public void addIdTokenListener(final String appName) {

FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);

if (!mIdTokenListeners.containsKey(appName)) {
FirebaseAuth.IdTokenListener newIdTokenListener =
firebaseAuth1 -> {
Expand Down Expand Up @@ -838,6 +840,45 @@ private void signInWithCredential(
});
}
}
@ReactMethod
public void signInWithProvider(String appName, String providerId, @Nullable String email, Promise promise){
OAuthProvider.Builder provider = OAuthProvider.newBuilder(providerId);
if(email != null){
provider.addCustomParameter("login_hint", email);
}
Activity activity = getCurrentActivity();
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);

OnSuccessListener onSuccess = new OnSuccessListener<AuthResult>(){
@Override
public void onSuccess(AuthResult authResult) {
Log.d(TAG, "signInWithProvider:onComplete:success");
promiseWithAuthResult(authResult, promise);
}
};

OnFailureListener onFailure = new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "signInWithProvider:onComplete:failure", e);
promiseRejectAuthException(promise, e);
}
};


Task<AuthResult> pendingResultTask = firebaseAuth.getPendingAuthResult();
if(pendingResultTask != null){
pendingResultTask
.addOnSuccessListener(onSuccess)
.addOnFailureListener(onFailure);
} else {
firebaseAuth
.startActivityForSignInWithProvider(activity, provider.build())
.addOnSuccessListener(onSuccess)
.addOnFailureListener(onFailure);
}
}

/**
* signInWithPhoneNumber
Expand Down
22 changes: 10 additions & 12 deletions packages/auth/e2e/auth.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,12 +912,11 @@ describe('auth()', function () {
});

describe('signInWithPopup', function () {
it('should throw an unsupported error', function () {
(() => {
firebase.auth().signInWithPopup();
}).should.throw(
'firebase.auth().signInWithPopup() is unsupported by the native Firebase SDKs.',
);
it('should trigger the oauth flow', async function () {
await (async () => {
const provider = new firebase.auth.OAuthProvider('oidc.react.com');
await firebase.auth().signInWithPopup(provider);
}).should.not.throw();
});
});

Expand Down Expand Up @@ -1025,12 +1024,11 @@ describe('auth()', function () {
});

describe('signInWithRedirect()', function () {
it('should throw an unsupported error', function () {
(() => {
firebase.auth().signInWithRedirect();
}).should.throw(
'firebase.auth().signInWithRedirect() is unsupported by the native Firebase SDKs.',
);
it('should trigger the oauth flow', async function () {
await (async () => {
const provider = new firebase.auth.OAuthProvider('oidc.react.com');
await firebase.auth().signInWithRedirect(provider);
}).should.not.throw();
});
});

Expand Down
4 changes: 1 addition & 3 deletions packages/auth/e2e/provider.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ describe('auth() -> Providers', function () {
describe('OAuthProvider', function () {
describe('constructor', function () {
it('should throw an unsupported error', function () {
(() => new firebase.auth.OAuthProvider()).should.throw(
'`new OAuthProvider()` is not supported on the native Firebase SDKs.',
);
(() => new firebase.auth.OAuthProvider('oidc.react.com')).should.not.throw();
});
});

Expand Down
38 changes: 32 additions & 6 deletions packages/auth/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,41 @@ class FirebaseAuthModule extends FirebaseModule {
throw new Error('firebase.auth().setPersistence() is unsupported by the native Firebase SDKs.');
}

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

signInWithRedirect() {
throw new Error(
'firebase.auth().signInWithRedirect() is unsupported by the native Firebase SDKs.',
return this.native
.signInWithProvider(provider.providerId, provider.customParameters?.login_hint)
.then(userCredential => this._setUserCredential(userCredential));
}

async linkWithPopup(provider) {
const credentials = await this.native.linkWithProvider(
provider.providerId,
provider.customParameters?.login_hint,
);

return this.native.linkWithCredential(
provider.providerId,
credentials.token,
credentials.secret,
);
}

async linkWithRedirect(provider) {
const credentials = await this.native.linkWithProvider(
provider.providerId,
provider.customParameters?.login_hint,
);

return this.native.linkWithCredential(
provider.providerId,
credentials.token,
credentials.secret,
);
}

Expand Down
10 changes: 8 additions & 2 deletions packages/auth/lib/providers/OAuthProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
const providerId = 'oauth';

export default class OAuthProvider {
constructor() {
throw new Error('`new OAuthProvider()` is not supported on the native Firebase SDKs.');
constructor(providerId) {
this.providerId = providerId;
}

customParameters = {};

setCustomParameters(customParameters) {
this.customParameters = customParameters;
}

static get PROVIDER_ID() {
Expand Down

0 comments on commit 805575a

Please sign in to comment.