Skip to content

Commit 21f16fd

Browse files
authored
request additional scopes if necessary (#146)
1 parent 56a9eec commit 21f16fd

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

ios/Plugin/Plugin.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class GoogleAuth: CAPPlugin {
1212
var googleSignIn: GIDSignIn!;
1313
var googleSignInConfiguration: GIDConfiguration!;
1414
var forceAuthCode: Bool = false;
15-
var scopes: [String]?;
15+
var additionalScopes: [String]!;
1616

1717

1818
public override func load() {
@@ -26,9 +26,15 @@ public class GoogleAuth: CAPPlugin {
2626
}
2727

2828
googleSignInConfiguration = GIDConfiguration.init(clientID: clientId, serverClientID: serverClientId)
29-
30-
scopes = getConfigValue("scopes") as? [String];
3129

30+
// these are scopes granted by default by the signIn method
31+
let defaultGrantedScopes = ["email", "profile", "openid"];
32+
33+
// these are scopes we will need to request after sign in
34+
additionalScopes = (getConfigValue("scopes") as? [String] ?? []).filter {
35+
return !defaultGrantedScopes.contains($0);
36+
};
37+
3238
if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
3339
forceAuthCode = forceAuthCodeConfig;
3440
}
@@ -48,12 +54,26 @@ public class GoogleAuth: CAPPlugin {
4854
if self.googleSignIn.hasPreviousSignIn() && !self.forceAuthCode {
4955
self.googleSignIn.restorePreviousSignIn();
5056
} else {
51-
self.googleSignIn.signIn(with: self.googleSignInConfiguration, presenting: self.bridge!.viewController!) { user, error in
57+
let presentingVc = self.bridge!.viewController!;
58+
59+
self.googleSignIn.signIn(with: self.googleSignInConfiguration, presenting: presentingVc) { user, error in
5260
if let error = error {
5361
self.signInCall?.reject(error.localizedDescription);
5462
return;
5563
}
56-
self.processCallback(user: user!);
64+
if self.additionalScopes.count > 0 {
65+
// requesting additional scopes in GoogleSignIn-iOS SDK 6.0 requires that you sign the user in and then request additional scopes,
66+
// there's no method to include the additional scopes in the initial sign in request
67+
self.googleSignIn.addScopes(self.additionalScopes, presenting: presentingVc) { user, error in
68+
if let error = error {
69+
self.signInCall?.reject(error.localizedDescription);
70+
return;
71+
}
72+
self.resolveSignInCallWith(user: user!);
73+
}
74+
} else {
75+
self.resolveSignInCallWith(user: user!);
76+
}
5777
};
5878
}
5979
}
@@ -125,7 +145,7 @@ public class GoogleAuth: CAPPlugin {
125145
return nil;
126146
}
127147

128-
func processCallback(user: GIDGoogleUser) {
148+
func resolveSignInCallWith(user: GIDGoogleUser) {
129149
var userData: [String: Any] = [
130150
"authentication": [
131151
"accessToken": user.authentication.accessToken,

0 commit comments

Comments
 (0)