Skip to content

Commit

Permalink
request additional scopes if necessary (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeflateau authored Sep 16, 2021
1 parent 56a9eec commit 21f16fd
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class GoogleAuth: CAPPlugin {
var googleSignIn: GIDSignIn!;
var googleSignInConfiguration: GIDConfiguration!;
var forceAuthCode: Bool = false;
var scopes: [String]?;
var additionalScopes: [String]!;


public override func load() {
Expand All @@ -26,9 +26,15 @@ public class GoogleAuth: CAPPlugin {
}

googleSignInConfiguration = GIDConfiguration.init(clientID: clientId, serverClientID: serverClientId)

scopes = getConfigValue("scopes") as? [String];

// these are scopes granted by default by the signIn method
let defaultGrantedScopes = ["email", "profile", "openid"];

// these are scopes we will need to request after sign in
additionalScopes = (getConfigValue("scopes") as? [String] ?? []).filter {
return !defaultGrantedScopes.contains($0);
};

if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
forceAuthCode = forceAuthCodeConfig;
}
Expand All @@ -48,12 +54,26 @@ public class GoogleAuth: CAPPlugin {
if self.googleSignIn.hasPreviousSignIn() && !self.forceAuthCode {
self.googleSignIn.restorePreviousSignIn();
} else {
self.googleSignIn.signIn(with: self.googleSignInConfiguration, presenting: self.bridge!.viewController!) { user, error in
let presentingVc = self.bridge!.viewController!;

self.googleSignIn.signIn(with: self.googleSignInConfiguration, presenting: presentingVc) { user, error in
if let error = error {
self.signInCall?.reject(error.localizedDescription);
return;
}
self.processCallback(user: user!);
if self.additionalScopes.count > 0 {
// requesting additional scopes in GoogleSignIn-iOS SDK 6.0 requires that you sign the user in and then request additional scopes,
// there's no method to include the additional scopes in the initial sign in request
self.googleSignIn.addScopes(self.additionalScopes, presenting: presentingVc) { user, error in
if let error = error {
self.signInCall?.reject(error.localizedDescription);
return;
}
self.resolveSignInCallWith(user: user!);
}
} else {
self.resolveSignInCallWith(user: user!);
}
};
}
}
Expand Down Expand Up @@ -125,7 +145,7 @@ public class GoogleAuth: CAPPlugin {
return nil;
}

func processCallback(user: GIDGoogleUser) {
func resolveSignInCallWith(user: GIDGoogleUser) {
var userData: [String: Any] = [
"authentication": [
"accessToken": user.authentication.accessToken,
Expand Down

0 comments on commit 21f16fd

Please sign in to comment.