Skip to content

Commit

Permalink
feat(authentication): provide access tokens for Google and Facebook (#55
Browse files Browse the repository at this point in the history
)
  • Loading branch information
robingenz authored Apr 2, 2022
1 parent 66b54ee commit 5c0f381
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-coins-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@capacitor-firebase/authentication": minor
---

feat: provide access tokens for Google and Facebook
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ Click on the name of the desired plugin under the [`Plugins`](#plugins) section

## Plugins

| Name | Package | Version | Downloads |
| ----------------------------------------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [Analytics](./packages/analytics/README.md#installation) | `@capacitor-firebase/analytics` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/analytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/analytics) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/analytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/analytics) |
| [App](./packages/app/README.md#installation) | `@capacitor-firebase/app` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/app?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/app) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/app?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/app) |
| [Authentication](./packages/authentication/README.md#installation) | `@capacitor-firebase/authentication` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/authentication?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/authentication) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/authentication?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/authentication) |
| [Crashlytics](./packages/crashlytics/README.md#installation) | `@capacitor-firebase/crashlytics` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/crashlytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/crashlytics) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/crashlytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/crashlytics) |
| [Performance Monitoring](./packages/performance/README.md#installation) | `@capacitor-firebase/performance` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/performance?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/performance) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/performance?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/performance) |
| Name | Package | Version | Downloads |
| ---------------------------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [Analytics](./packages/analytics/README.md) | `@capacitor-firebase/analytics` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/analytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/analytics) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/analytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/analytics) |
| [App](./packages/app/README.md) | `@capacitor-firebase/app` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/app?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/app) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/app?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/app) |
| [Authentication](./packages/authentication/README.md) | `@capacitor-firebase/authentication` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/authentication?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/authentication) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/authentication?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/authentication) |
| [Crashlytics](./packages/crashlytics/README.md) | `@capacitor-firebase/crashlytics` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/crashlytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/crashlytics) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/crashlytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/crashlytics) |
| [Performance Monitoring](./packages/performance/README.md) | `@capacitor-firebase/performance` | [![npm badge](https://img.shields.io/npm/v/@capacitor-firebase/performance?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/performance) | [![npm downloads](https://img.shields.io/npm/dw/@capacitor-firebase/performance?style=flat-square)](https://www.npmjs.com/package/@capacitor-firebase/performance) |

## License

Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(FirebaseAuthenticationPlugin.TAG, "signInWithCustomToken succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null);
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null);
call.resolve(signInResult);
} else {
Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithCustomToken failed.", task.getException());
Expand Down Expand Up @@ -213,13 +213,19 @@ public void handleOnActivityResult(int requestCode, int resultCode, Intent data)
}

public void handleSuccessfulSignIn(final PluginCall call, AuthCredential credential, String idToken) {
handleSuccessfulSignIn(call, credential, idToken, null);
handleSuccessfulSignIn(call, credential, idToken, null, null);
}

public void handleSuccessfulSignIn(final PluginCall call, AuthCredential credential, String idToken, String nonce) {
public void handleSuccessfulSignIn(
final PluginCall call,
@Nullable AuthCredential credential,
@Nullable String idToken,
@Nullable String nonce,
@Nullable String accessToken
) {
boolean skipNativeAuth = this.config.getSkipNativeAuth();
if (skipNativeAuth) {
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(null, credential, idToken, nonce);
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(null, credential, idToken, nonce, accessToken);
call.resolve(signInResult);
return;
}
Expand All @@ -233,7 +239,13 @@ public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(FirebaseAuthenticationPlugin.TAG, "signInWithCredential succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, credential, idToken, nonce);
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(
user,
credential,
idToken,
nonce,
accessToken
);
call.resolve(signInResult);
} else {
Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithCredential failed.", task.getException());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package dev.robingenz.capacitorjs.plugins.firebase.authentication;

import androidx.annotation.Nullable;
import com.getcapacitor.JSObject;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
import com.google.firebase.auth.GoogleAuthCredential;
import com.google.firebase.auth.OAuthCredential;
import com.google.firebase.auth.PhoneAuthCredential;
import dev.robingenz.capacitorjs.plugins.firebase.authentication.handlers.GoogleAuthProviderHandler;
import dev.robingenz.capacitorjs.plugins.firebase.authentication.handlers.OAuthProviderHandler;

public class FirebaseAuthenticationHelper {

public static JSObject createSignInResult(FirebaseUser user, AuthCredential credential, String idToken) {
return createSignInResult(user, credential, idToken, null);
}

public static JSObject createSignInResult(FirebaseUser user, AuthCredential credential, String idToken, String nonce) {
public static JSObject createSignInResult(
@Nullable FirebaseUser user,
@Nullable AuthCredential credential,
@Nullable String idToken,
@Nullable String nonce,
@Nullable String accessToken
) {
JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user);
JSObject credentialResult = FirebaseAuthenticationHelper.createCredentialResult(credential, idToken, nonce);
JSObject credentialResult = FirebaseAuthenticationHelper.createCredentialResult(credential, idToken, nonce, accessToken);
JSObject result = new JSObject();
result.put("user", userResult);
result.put("credential", credentialResult);
return result;
}

public static JSObject createUserResult(FirebaseUser user) {
public static JSObject createUserResult(@Nullable FirebaseUser user) {
if (user == null) {
return null;
}
Expand All @@ -42,8 +40,13 @@ public static JSObject createUserResult(FirebaseUser user) {
return result;
}

public static JSObject createCredentialResult(AuthCredential credential, String idToken, String nonce) {
if (credential == null && idToken == null) {
public static JSObject createCredentialResult(
@Nullable AuthCredential credential,
@Nullable String idToken,
@Nullable String nonce,
@Nullable String accessToken
) {
if (credential == null && idToken == null && nonce == null && accessToken == null) {
return null;
}
JSObject result = new JSObject();
Expand All @@ -70,6 +73,9 @@ public static JSObject createCredentialResult(AuthCredential credential, String
if (nonce != null) {
result.put("nonce", nonce);
}
if (accessToken != null) {
result.put("accessToken", accessToken);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void startActivityForSignIn(final PluginCall call, OAuthProvider.Builder
.addOnSuccessListener(
authResult -> {
AuthCredential credential = authResult.getCredential();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce);
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce, null);
}
)
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
Expand All @@ -97,7 +97,7 @@ private void finishActivityForSignIn(final PluginCall call, Task<AuthResult> pen
.addOnSuccessListener(
authResult -> {
AuthCredential credential = authResult.getCredential();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce);
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce, null);
}
)
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public void handleOnActivityResult(int requestCode, int resultCode, Intent data)

private void handleSuccessCallback(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
String token = accessToken.getToken();
AuthCredential credential = FacebookAuthProvider.getCredential(token);
pluginImplementation.handleSuccessfulSignIn(savedCall, credential, token);
String accessTokenString = accessToken.getToken();
AuthCredential credential = FacebookAuthProvider.getCredential(accessTokenString);
pluginImplementation.handleSuccessfulSignIn(savedCall, credential, accessTokenString, null, accessTokenString);
}

private void handleCancelCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void onVerificationFailed(FirebaseException exception) {

@Override
public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) {
JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null);
JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null, null);
result.put("verificationId", verificationId);
call.resolve(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public typealias AuthStateChangedObserver = () -> Void
return
}
let user = self.getCurrentUser()
let result = FirebaseAuthenticationHelper.createSignInResult(credential: nil, user: user, idToken: nil, nonce: nil)
let result = FirebaseAuthenticationHelper.createSignInResult(credential: nil, user: user, idToken: nil, nonce: nil, accessToken: nil)
savedCall.resolve(result)
}
}
Expand All @@ -132,12 +132,12 @@ public typealias AuthStateChangedObserver = () -> Void
Auth.auth().useEmulator(withHost: host, port: port)
}

func handleSuccessfulSignIn(credential: AuthCredential, idToken: String?, nonce: String?) {
func handleSuccessfulSignIn(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?) {
if config.skipNativeAuth == true {
guard let savedCall = self.savedCall else {
return
}
let result = FirebaseAuthenticationHelper.createSignInResult(credential: credential, user: nil, idToken: idToken, nonce: nonce)
let result = FirebaseAuthenticationHelper.createSignInResult(credential: credential, user: nil, idToken: idToken, nonce: nonce, accessToken: accessToken)
savedCall.resolve(result)
return
}
Expand All @@ -150,7 +150,7 @@ public typealias AuthStateChangedObserver = () -> Void
return
}
let user = self.getCurrentUser()
let result = FirebaseAuthenticationHelper.createSignInResult(credential: credential, user: user, idToken: idToken, nonce: nonce)
let result = FirebaseAuthenticationHelper.createSignInResult(credential: credential, user: user, idToken: idToken, nonce: nonce, accessToken: accessToken)
savedCall.resolve(result)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import FirebaseCore
import FirebaseAuth

public class FirebaseAuthenticationHelper {
public static func createSignInResult(credential: AuthCredential?, user: User?, idToken: String?, nonce: String?) -> JSObject {
public static func createSignInResult(credential: AuthCredential?, user: User?, idToken: String?, nonce: String?, accessToken: String?) -> JSObject {
let userResult = self.createUserResult(user)
let credentialResult = self.createCredentialResult(credential, idToken: idToken, nonce: nonce)
let credentialResult = self.createCredentialResult(credential, idToken: idToken, nonce: nonce, accessToken: accessToken)
var result = JSObject()
result["user"] = userResult
result["credential"] = credentialResult
Expand All @@ -30,8 +30,8 @@ public class FirebaseAuthenticationHelper {
return result
}

public static func createCredentialResult(_ credential: AuthCredential?, idToken: String?, nonce: String?) -> JSObject? {
if credential == nil && idToken == nil && nonce == nil {
public static func createCredentialResult(_ credential: AuthCredential?, idToken: String?, nonce: String?, accessToken: String?) -> JSObject? {
if credential == nil && idToken == nil && nonce == nil && accessToken == nil {
return nil
}
var result = JSObject()
Expand All @@ -58,6 +58,9 @@ public class FirebaseAuthenticationHelper {
if let nonce = nonce {
result["nonce"] = nonce
}
if let accessToken = accessToken {
result["accessToken"] = accessToken
}
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extension AppleAuthProviderHandler: ASAuthorizationControllerDelegate, ASAuthori
return
}
let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
self.pluginImplementation.handleSuccessfulSignIn(credential: credential, idToken: idTokenString, nonce: nonce)
self.pluginImplementation.handleSuccessfulSignIn(credential: credential, idToken: idTokenString, nonce: nonce, accessToken: nil)
}

func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
Expand Down
Loading

0 comments on commit 5c0f381

Please sign in to comment.