Skip to content

Commit

Permalink
refactor(authentication): minor changes (#97)
Browse files Browse the repository at this point in the history
* refactor(authentication): minor changes

* style: format
  • Loading branch information
robingenz authored May 21, 2022
1 parent 37485bc commit a51927f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private String sha256(String s) throws NoSuchAlgorithmException {

public void signIn(PluginCall call) {
OAuthProvider.Builder provider = OAuthProvider.newBuilder("apple.com");
applySignInConfig(call, provider);
applySignInOptions(call, provider);
Task<AuthResult> pendingResultTask = pluginImplementation.getFirebaseAuthInstance().getPendingAuthResult();
if (pendingResultTask == null) {
currentNonce = generateNonce(32);
Expand Down Expand Up @@ -103,7 +103,7 @@ private void finishActivityForSignIn(final PluginCall call, Task<AuthResult> pen
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
}

private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) {
private void applySignInOptions(PluginCall call, OAuthProvider.Builder provider) {
JSArray customParameters = call.getArray("customParameters");
if (customParameters != null) {
try {
Expand All @@ -118,7 +118,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider)
provider.addCustomParameter(key, value);
}
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception);
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception);
}
}

Expand All @@ -128,7 +128,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider)
List<String> scopeList = scopes.toList();
provider.setScopes(scopeList);
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception);
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,9 @@ public void onError(FacebookException exception) {
}
}

private void applySignInConfig(LoginButton button) {
JSArray scopes = this.savedCall.getArray("scopes");
if (scopes != null) {
try {
List<String> scopeList = scopes.toList();
scopeList.add("email");
scopeList.add("public_profile");
button.setPermissions(scopeList);
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception);
}
}
}

public void signIn(PluginCall call) {
this.savedCall = call;
this.applySignInConfig(this.loginButton);
this.applySignInOptions(call, this.loginButton);
this.loginButton.performClick();
}

Expand All @@ -87,6 +73,20 @@ public void handleOnActivityResult(int requestCode, int resultCode, Intent data)
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}

private void applySignInOptions(PluginCall call, LoginButton button) {
JSArray scopes = call.getArray("scopes");
if (scopes != null) {
try {
List<String> scopeList = scopes.toList();
scopeList.add("email");
scopeList.add("public_profile");
button.setPermissions(scopeList);
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception);
}
}
}

private void handleSuccessCallback(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
String accessTokenString = accessToken.getToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class GoogleAuthProviderHandler {

public GoogleAuthProviderHandler(FirebaseAuthentication pluginImplementation) {
this.pluginImplementation = pluginImplementation;
this.buildGoogleSignInClient(null);
this.mGoogleSignInClient = buildGoogleSignInClient();
}

public void signIn(PluginCall call) {
this.buildGoogleSignInClient(call);
mGoogleSignInClient = buildGoogleSignInClient(call);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
pluginImplementation.startActivityForResult(call, signInIntent, "handleGoogleAuthProviderActivityResult");
}
Expand All @@ -56,7 +56,11 @@ public void handleOnActivityResult(PluginCall call, ActivityResult result) {
}
}

private void buildGoogleSignInClient(@Nullable PluginCall call) {
private GoogleSignInClient buildGoogleSignInClient() {
return buildGoogleSignInClient(null);
}

private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) {
GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id))
.requestEmail();
Expand All @@ -75,6 +79,6 @@ private void buildGoogleSignInClient(@Nullable PluginCall call) {
}
}

mGoogleSignInClient = GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build());
return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public OAuthProviderHandler(FirebaseAuthentication pluginImplementation) {

public void signIn(PluginCall call, String providerId) {
OAuthProvider.Builder provider = OAuthProvider.newBuilder(providerId);
applySignInConfig(call, provider);
applySignInOptions(call, provider);
Task<AuthResult> pendingResultTask = pluginImplementation.getFirebaseAuthInstance().getPendingAuthResult();
if (pendingResultTask == null) {
startActivityForSignIn(call, provider);
Expand Down Expand Up @@ -58,7 +58,7 @@ private void finishActivityForSignIn(final PluginCall call, Task<AuthResult> pen
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
}

private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) {
private void applySignInOptions(PluginCall call, OAuthProvider.Builder provider) {
JSArray customParameters = call.getArray("customParameters");
if (customParameters != null) {
try {
Expand All @@ -73,7 +73,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider)
provider.addCustomParameter(key, value);
}
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception);
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception);
}
}

Expand All @@ -83,7 +83,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider)
List<String> scopeList = scopes.toList();
provider.setScopes(scopeList);
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception);
Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,11 @@ public class PlayGamesAuthProviderHandler {

public PlayGamesAuthProviderHandler(FirebaseAuthentication pluginImplementation) {
this.pluginImplementation = pluginImplementation;
this.buildGoogleSignInClient(null);
}

private void buildGoogleSignInClient(@Nullable PluginCall call) {
GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id))
.requestEmail();

if (call != null) {
JSArray scopes = call.getArray("scopes");
if (scopes != null) {
try {
List<String> scopeList = scopes.toList();
for (String scope : scopeList) {
gsob = gsob.requestScopes(new Scope(scope));
}
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "buildGoogleSignInClient failed.", exception);
}
}
}

mGoogleSignInClient = GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build());
this.mGoogleSignInClient = buildGoogleSignInClient();
}

public void signIn(PluginCall call) {
this.buildGoogleSignInClient(call);
this.mGoogleSignInClient = buildGoogleSignInClient(call);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
pluginImplementation.startActivityForResult(call, signInIntent, "handlePlayGamesAuthProviderActivityResult");
}
Expand All @@ -76,4 +54,30 @@ public void handleOnActivityResult(PluginCall call, ActivityResult result) {
pluginImplementation.handleFailedSignIn(call, null, exception);
}
}

private GoogleSignInClient buildGoogleSignInClient() {
return buildGoogleSignInClient(null);
}

private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) {
GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id))
.requestEmail();

if (call != null) {
JSArray scopes = call.getArray("scopes");
if (scopes != null) {
try {
List<String> scopeList = scopes.toList();
for (String scope : scopeList) {
gsob = gsob.requestScopes(new Scope(scope));
}
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "buildGoogleSignInClient failed.", exception);
}
}
}

return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class OAuthProviderHandler: NSObject {

func signIn(call: CAPPluginCall, providerId: String) {
self.provider = OAuthProvider(providerID: providerId)
self.applySignInConfig(call: call, provider: provider!)
self.applySignInOptions(call: call, provider: provider!)
DispatchQueue.main.async {
self.startSignInFlow()
}
}

private func applySignInConfig(call: CAPPluginCall, provider: OAuthProvider) {
private func applySignInOptions(call: CAPPluginCall, provider: OAuthProvider) {
let customParameters = call.getArray("customParameters", JSObject.self) ?? []
for (_, customParameter) in customParameters.enumerated() {
guard let key = customParameter["key"] as? String else {
Expand Down
36 changes: 18 additions & 18 deletions packages/authentication/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,9 @@ export class FirebaseAuthenticationWeb
auth.languageCode = options.languageCode;
}

private applySignInConfig(
provider: OAuthProvider | GoogleAuthProvider | FacebookAuthProvider,
options?: SignInOptions,
) {
if (options?.scopes) {
for (const scope of options.scopes) {
provider.addScope(scope);
}
}
}

public async signInWithApple(options?: SignInOptions): Promise<SignInResult> {
const provider = new OAuthProvider('apple.com');
this.applySignInConfig(provider, options);
this.applySignInOptions(options || {}, provider);
const auth = getAuth();
const result = await signInWithPopup(auth, provider);
const credential = OAuthProvider.credentialFromResult(result);
Expand Down Expand Up @@ -169,7 +158,7 @@ export class FirebaseAuthenticationWeb
options?: SignInOptions,
): Promise<SignInResult> {
const provider = new FacebookAuthProvider();
this.applySignInConfig(provider, options);
this.applySignInOptions(options || {}, provider);
const auth = getAuth();
const result = await signInWithPopup(auth, provider);
const credential = FacebookAuthProvider.credentialFromResult(result);
Expand All @@ -180,7 +169,7 @@ export class FirebaseAuthenticationWeb
options?: SignInOptions,
): Promise<SignInResult> {
const provider = new OAuthProvider('github.com');
this.applySignInConfig(provider, options);
this.applySignInOptions(options || {}, provider);
const auth = getAuth();
const result = await signInWithPopup(auth, provider);
const credential = OAuthProvider.credentialFromResult(result);
Expand All @@ -191,7 +180,7 @@ export class FirebaseAuthenticationWeb
options?: SignInOptions,
): Promise<SignInResult> {
const provider = new GoogleAuthProvider();
this.applySignInConfig(provider, options);
this.applySignInOptions(options || {}, provider);
const auth = getAuth();
const result = await signInWithPopup(auth, provider);
const credential = GoogleAuthProvider.credentialFromResult(result);
Expand All @@ -202,7 +191,7 @@ export class FirebaseAuthenticationWeb
options?: SignInOptions,
): Promise<SignInResult> {
const provider = new OAuthProvider('microsoft.com');
this.applySignInConfig(provider, options);
this.applySignInOptions(options || {}, provider);
const auth = getAuth();
const result = await signInWithPopup(auth, provider);
const credential = OAuthProvider.credentialFromResult(result);
Expand All @@ -223,7 +212,7 @@ export class FirebaseAuthenticationWeb
options?: SignInOptions,
): Promise<SignInResult> {
const provider = new OAuthProvider('twitter.com');
this.applySignInConfig(provider, options);
this.applySignInOptions(options || {}, provider);
const auth = getAuth();
const result = await signInWithPopup(auth, provider);
const credential = OAuthProvider.credentialFromResult(result);
Expand All @@ -232,7 +221,7 @@ export class FirebaseAuthenticationWeb

public async signInWithYahoo(options?: SignInOptions): Promise<SignInResult> {
const provider = new OAuthProvider('yahoo.com');
this.applySignInConfig(provider, options);
this.applySignInOptions(options || {}, provider);
const auth = getAuth();
const result = await signInWithPopup(auth, provider);
const credential = OAuthProvider.credentialFromResult(result);
Expand Down Expand Up @@ -281,6 +270,17 @@ export class FirebaseAuthenticationWeb
this.notifyListeners('authStateChange', change);
}

private applySignInOptions(
options: SignInOptions,
provider: OAuthProvider | GoogleAuthProvider | FacebookAuthProvider,
) {
if (options?.scopes) {
for (const scope of options.scopes) {
provider.addScope(scope);
}
}
}

private createSignInResultFromAuthCredential(
user: FirebaseUser,
credential: FirebaseAuthCredential | null,
Expand Down

0 comments on commit a51927f

Please sign in to comment.