Skip to content

Commit 4859f9c

Browse files
joeflateaureslear
andauthored
Update ios to GoogleSignIn 6.0.1 Pod (#136)
* allow clientId to be set from capacitor config rather than google services plist * accept different android/ios client ids * update to google auth 6.0.1 * update podfile lock * need to update the podspec * update ios plugin/bridge for v6 * some versions of xcode don't like this guard * silence some swift compiler warnings * docs: add information about `Client ID` * Update Plugin.swift Co-authored-by: reslear <[email protected]>
1 parent 44d766b commit 4859f9c

File tree

7 files changed

+112
-123
lines changed

7 files changed

+112
-123
lines changed

CodetrixStudioCapacitorGoogleAuth.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
1111
s.ios.deployment_target = '12.0'
1212
s.dependency 'Capacitor'
13-
s.dependency 'GoogleSignIn', '~> 5.0.2'
13+
s.dependency 'GoogleSignIn', '~> 6.0.1'
1414
s.static_framework = true
1515
end

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,28 @@ or see more [CapacitorGoogleAuth-Vue3-example](https://github.com/reslear/Capaci
115115

116116
### iOS
117117

118-
Make sure you have `GoogleService-Info.plist` with `CLIENT_ID`
118+
1. Create in Google cloud console credential **Client ID for iOS** and get **Client ID** and **iOS URL scheme**
119+
120+
2. Add **identifier** `REVERSED_CLIENT_ID` as **URL schemes** to `Info.plist` from **iOS URL scheme**<br>
121+
(Xcode: App - Targets/App - Info - URL Types, click plus icon)
122+
123+
3. Set **Client ID** one of the ways:
124+
1. Set in `capacitor.config.json`
125+
- `iosClientId` - specific key for iOS
126+
- `clientId` - or common key for Android and iOS
127+
3. Download `GoogleService-Info.plist` file with `CLIENT_ID` and copy to **ios/App/App** necessarily through Xcode for indexing.
128+
119129

120-
Add `REVERSED_CLIENT_ID` as url scheme to `Info.plist`
121130

122131
### Android
123132

124-
Inside your `strings.xml`
133+
Set **Client ID** :
134+
135+
1. In `capacitor.config.json`
136+
- `androidClientId` - specific key for Android
137+
- `clientId` - or common key for Android and iOS
125138

139+
2. or set inside your `strings.xml`
126140
```xml
127141
<resources>
128142
<string name="server_client_id">Your Web Client Key</string>

android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ public class GoogleAuth extends Plugin {
2727

2828
@Override
2929
public void load() {
30-
String clientId = this.getContext().getString(R.string.server_client_id);
31-
boolean forceCodeForRefreshToken = false;
30+
String clientId = getConfig().getString("androidClientId",
31+
getConfig().getString("clientId",
32+
this.getContext().getString(R.string.server_client_id)));
3233

33-
Boolean forceRefreshToken = (Boolean) getConfigValue("forceCodeForRefreshToken");
34-
if (forceRefreshToken != null) {
35-
forceCodeForRefreshToken = forceRefreshToken;
36-
}
34+
boolean forceCodeForRefreshToken = getConfig().getBoolean("forceCodeForRefreshToken", false);
3735

3836
GoogleSignInOptions.Builder googleSignInBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
3937
.requestIdToken(clientId)
@@ -43,17 +41,13 @@ public void load() {
4341
googleSignInBuilder.requestServerAuthCode(clientId, true);
4442
}
4543

46-
try {
47-
JSONArray scopeArray = (JSONArray) getConfigValue("scopes");
48-
Scope[] scopes = new Scope[scopeArray.length() - 1];
49-
Scope firstScope = new Scope(scopeArray.getString(0));
50-
for (int i = 1; i < scopeArray.length(); i++) {
51-
scopes[i - 1] = new Scope(scopeArray.getString(i));
52-
}
53-
googleSignInBuilder.requestScopes(firstScope, scopes);
54-
} catch (JSONException e) {
55-
e.printStackTrace();
44+
String[] scopeArray = getConfig().getArray("scopes", new String[] {});
45+
Scope[] scopes = new Scope[scopeArray.length - 1];
46+
Scope firstScope = new Scope(scopeArray[0]);
47+
for (int i = 1; i < scopeArray.length; i++) {
48+
scopes[i - 1] = new Scope(scopeArray[i]);
5649
}
50+
googleSignInBuilder.requestScopes(firstScope, scopes);
5751

5852
GoogleSignInOptions googleSignInOptions = googleSignInBuilder.build();
5953
googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions);

ios/Plugin.xcodeproj/project.pbxproj

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@
151151
50ADFF84201F53D600D50D53 /* Frameworks */,
152152
50ADFF85201F53D600D50D53 /* Headers */,
153153
50ADFF86201F53D600D50D53 /* Resources */,
154-
D5C0AD06C6436A87FC8525B9 /* [CP] Copy Pods Resources */,
155154
);
156155
buildRules = (
157156
);
@@ -171,7 +170,6 @@
171170
50ADFF8E201F53D600D50D53 /* Frameworks */,
172171
50ADFF8F201F53D600D50D53 /* Resources */,
173172
CDBD6291F99392856E80ED8A /* [CP] Embed Pods Frameworks */,
174-
954E831AAF3D05995C5AB46F /* [CP] Copy Pods Resources */,
175173
);
176174
buildRules = (
177175
);
@@ -262,24 +260,6 @@
262260
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
263261
showEnvVarsInLog = 0;
264262
};
265-
954E831AAF3D05995C5AB46F /* [CP] Copy Pods Resources */ = {
266-
isa = PBXShellScriptBuildPhase;
267-
buildActionMask = 2147483647;
268-
files = (
269-
);
270-
inputPaths = (
271-
"${PODS_ROOT}/Target Support Files/Pods-PluginTests/Pods-PluginTests-resources.sh",
272-
"${PODS_ROOT}/GoogleSignIn/Resources/GoogleSignIn.bundle",
273-
);
274-
name = "[CP] Copy Pods Resources";
275-
outputPaths = (
276-
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/GoogleSignIn.bundle",
277-
);
278-
runOnlyForDeploymentPostprocessing = 0;
279-
shellPath = /bin/sh;
280-
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-PluginTests/Pods-PluginTests-resources.sh\"\n";
281-
showEnvVarsInLog = 0;
282-
};
283263
AB5B3E54B4E897F32C2279DA /* [CP] Check Pods Manifest.lock */ = {
284264
isa = PBXShellScriptBuildPhase;
285265
buildActionMask = 2147483647;
@@ -310,6 +290,7 @@
310290
"${BUILT_PRODUCTS_DIR}/AppAuth/AppAuth.framework",
311291
"${BUILT_PRODUCTS_DIR}/GTMAppAuth/GTMAppAuth.framework",
312292
"${BUILT_PRODUCTS_DIR}/GTMSessionFetcher/GTMSessionFetcher.framework",
293+
"${BUILT_PRODUCTS_DIR}/GoogleSignIn/GoogleSignIn.framework",
313294
);
314295
name = "[CP] Embed Pods Frameworks";
315296
outputPaths = (
@@ -318,30 +299,13 @@
318299
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/AppAuth.framework",
319300
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GTMAppAuth.framework",
320301
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GTMSessionFetcher.framework",
302+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GoogleSignIn.framework",
321303
);
322304
runOnlyForDeploymentPostprocessing = 0;
323305
shellPath = /bin/sh;
324306
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-PluginTests/Pods-PluginTests-frameworks.sh\"\n";
325307
showEnvVarsInLog = 0;
326308
};
327-
D5C0AD06C6436A87FC8525B9 /* [CP] Copy Pods Resources */ = {
328-
isa = PBXShellScriptBuildPhase;
329-
buildActionMask = 2147483647;
330-
files = (
331-
);
332-
inputPaths = (
333-
"${PODS_ROOT}/Target Support Files/Pods-Plugin/Pods-Plugin-resources.sh",
334-
"${PODS_ROOT}/GoogleSignIn/Resources/GoogleSignIn.bundle",
335-
);
336-
name = "[CP] Copy Pods Resources";
337-
outputPaths = (
338-
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/GoogleSignIn.bundle",
339-
);
340-
runOnlyForDeploymentPostprocessing = 0;
341-
shellPath = /bin/sh;
342-
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Plugin/Pods-Plugin-resources.sh\"\n";
343-
showEnvVarsInLog = 0;
344-
};
345309
/* End PBXShellScriptBuildPhase section */
346310

347311
/* Begin PBXSourcesBuildPhase section */

ios/Plugin/Plugin.swift

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,36 @@ import GoogleSignIn
88
*/
99
@objc(GoogleAuth)
1010
public class GoogleAuth: CAPPlugin {
11-
var signInCall: CAPPluginCall?
12-
let googleSignIn: GIDSignIn = GIDSignIn.sharedInstance();
11+
var signInCall: CAPPluginCall!
12+
var googleSignIn: GIDSignIn!;
13+
var googleSignInConfiguration: GIDConfiguration!;
1314
var forceAuthCode: Bool = false;
15+
var scopes: [String]?;
1416

17+
1518
public override func load() {
16-
guard let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") else {
17-
print("GoogleService-Info.plist not found");
19+
googleSignIn = GIDSignIn.sharedInstance;
20+
21+
let serverClientId = getServerClientIdValue();
22+
23+
guard let clientId = getClientIdValue() else {
24+
NSLog("no client id found in config")
1825
return;
1926
}
20-
guard let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] else {return}
21-
guard let clientId = dict["CLIENT_ID"] as? String else {return}
22-
googleSignIn.clientID = clientId;
23-
googleSignIn.delegate = self;
24-
googleSignIn.presentingViewController = bridge?.viewController;
25-
if let serverClientId = getConfigValue("serverClientId") as? String {
26-
googleSignIn.serverClientID = serverClientId;
27-
}
28-
if let scopes = getConfigValue("scopes") as? [String] {
29-
googleSignIn.scopes = scopes;
30-
}
27+
28+
googleSignInConfiguration = GIDConfiguration.init(clientID: clientId, serverClientID: serverClientId)
29+
30+
scopes = getConfigValue("scopes") as? [String];
31+
3132
if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
3233
forceAuthCode = forceAuthCodeConfig;
3334
}
34-
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(CAPNotifications.URLOpen.name()), object: nil);
35+
36+
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(Notification.Name.capacitorOpenURL.rawValue), object: nil);
3537
}
3638

3739
@objc
38-
func init(_ call: CAPPluginCall) {
40+
func `init`(_ call: CAPPluginCall) {
3941
call.unimplemented("Not available on iOS")
4042
}
4143

@@ -46,7 +48,13 @@ public class GoogleAuth: CAPPlugin {
4648
if self.googleSignIn.hasPreviousSignIn() && !self.forceAuthCode {
4749
self.googleSignIn.restorePreviousSignIn();
4850
} else {
49-
self.googleSignIn.signIn();
51+
self.googleSignIn.signIn(with: self.googleSignInConfiguration, presenting: self.bridge!.viewController!) { user, error in
52+
if let error = error {
53+
self.signInCall?.reject(error.localizedDescription);
54+
return;
55+
}
56+
self.processCallback(user: user!);
57+
};
5058
}
5159
}
5260
}
@@ -58,14 +66,14 @@ public class GoogleAuth: CAPPlugin {
5866
call.reject("User not logged in.");
5967
return
6068
}
61-
self.googleSignIn.currentUser.authentication.getTokensWithHandler { (authentication, error) in
69+
self.googleSignIn.currentUser!.authentication.do { (authentication, error) in
6270
guard let authentication = authentication else {
6371
call.reject(error?.localizedDescription ?? "Something went wrong.");
6472
return;
6573
}
6674
let authenticationData: [String: Any] = [
6775
"accessToken": authentication.accessToken,
68-
"idToken": authentication.idToken,
76+
"idToken": authentication.idToken ?? NSNull(),
6977
"refreshToken": authentication.refreshToken
7078
]
7179
call.resolve(authenticationData);
@@ -93,6 +101,29 @@ public class GoogleAuth: CAPPlugin {
93101
}
94102
googleSignIn.handle(url);
95103
}
104+
105+
106+
func getClientIdValue() -> String? {
107+
if let clientId = getConfigValue("iosClientId") as? String {
108+
return clientId;
109+
}
110+
else if let clientId = getConfigValue("clientId") as? String {
111+
return clientId;
112+
}
113+
else if let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist"),
114+
let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject],
115+
let clientId = dict["CLIENT_ID"] as? String {
116+
return clientId;
117+
}
118+
return nil;
119+
}
120+
121+
func getServerClientIdValue() -> String? {
122+
if let serverClientId = getConfigValue("serverClientId") as? String {
123+
return serverClientId;
124+
}
125+
return nil;
126+
}
96127

97128
func processCallback(user: GIDGoogleUser) {
98129
var userData: [String: Any] = [
@@ -101,26 +132,16 @@ public class GoogleAuth: CAPPlugin {
101132
"idToken": user.authentication.idToken,
102133
"refreshToken": user.authentication.refreshToken
103134
],
104-
"serverAuthCode": user.serverAuthCode,
105-
"email": user.profile.email,
106-
"familyName": user.profile.familyName,
107-
"givenName": user.profile.givenName,
108-
"id": user.userID,
109-
"name": user.profile.name
135+
"serverAuthCode": user.serverAuthCode ?? NSNull(),
136+
"email": user.profile?.email ?? NSNull(),
137+
"familyName": user.profile?.familyName ?? NSNull(),
138+
"givenName": user.profile?.givenName ?? NSNull(),
139+
"id": user.userID ?? NSNull(),
140+
"name": user.profile?.name ?? NSNull()
110141
];
111-
if let imageUrl = user.profile.imageURL(withDimension: 100)?.absoluteString {
142+
if let imageUrl = user.profile?.imageURL(withDimension: 100)?.absoluteString {
112143
userData["imageUrl"] = imageUrl;
113144
}
114145
signInCall?.resolve(userData);
115146
}
116147
}
117-
118-
extension GoogleAuth: GIDSignInDelegate {
119-
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
120-
if let error = error {
121-
signInCall?.reject(error.localizedDescription);
122-
return;
123-
}
124-
processCallback(user: user);
125-
}
126-
}

ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ target 'Plugin' do
66
pod 'Capacitor', :path => '../node_modules/@capacitor/ios'
77
pod 'CapacitorCordova', :path => '../node_modules/@capacitor/ios'
88

9-
pod 'GoogleSignIn', '~> 5.0.2'
9+
pod 'GoogleSignIn', '~> 6.0.1'
1010

1111
end
1212

ios/Podfile.lock

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
PODS:
2-
- AppAuth (1.3.0):
3-
- AppAuth/Core (= 1.3.0)
4-
- AppAuth/ExternalUserAgent (= 1.3.0)
5-
- AppAuth/Core (1.3.0)
6-
- AppAuth/ExternalUserAgent (1.3.0)
7-
- Capacitor (1.5.1):
8-
- CapacitorCordova (= 1.5.1)
9-
- CapacitorCordova (1.5.1)
10-
- GoogleSignIn (5.0.2):
11-
- AppAuth (~> 1.2)
2+
- AppAuth (1.4.0):
3+
- AppAuth/Core (= 1.4.0)
4+
- AppAuth/ExternalUserAgent (= 1.4.0)
5+
- AppAuth/Core (1.4.0)
6+
- AppAuth/ExternalUserAgent (1.4.0)
7+
- Capacitor (3.0.1):
8+
- CapacitorCordova
9+
- CapacitorCordova (3.0.1)
10+
- GoogleSignIn (6.0.1):
11+
- AppAuth (~> 1.4)
1212
- GTMAppAuth (~> 1.0)
1313
- GTMSessionFetcher/Core (~> 1.1)
14-
- GTMAppAuth (1.0.0):
15-
- AppAuth/Core (~> 1.0)
16-
- GTMSessionFetcher (~> 1.1)
17-
- GTMSessionFetcher (1.3.1):
18-
- GTMSessionFetcher/Full (= 1.3.1)
19-
- GTMSessionFetcher/Core (1.3.1)
20-
- GTMSessionFetcher/Full (1.3.1):
21-
- GTMSessionFetcher/Core (= 1.3.1)
14+
- GTMAppAuth (1.2.2):
15+
- AppAuth/Core (~> 1.4)
16+
- GTMSessionFetcher/Core (~> 1.5)
17+
- GTMSessionFetcher/Core (1.6.1)
2218

2319
DEPENDENCIES:
2420
- "Capacitor (from `../node_modules/@capacitor/ios`)"
2521
- "CapacitorCordova (from `../node_modules/@capacitor/ios`)"
26-
- GoogleSignIn (~> 5.0.2)
22+
- GoogleSignIn (~> 6.0.1)
2723

2824
SPEC REPOS:
2925
trunk:
@@ -39,13 +35,13 @@ EXTERNAL SOURCES:
3935
:path: "../node_modules/@capacitor/ios"
4036

4137
SPEC CHECKSUMS:
42-
AppAuth: 73574f3013a1e65b9601a3ddc8b3158cce68c09d
43-
Capacitor: 373adb05f4591a70c22adfe979b5a443ac1e9b0a
44-
CapacitorCordova: 169792fba40cc325d70b3cbca49d0563acdf5924
45-
GoogleSignIn: 7137d297ddc022a7e0aa4619c86d72c909fa7213
46-
GTMAppAuth: 4deac854479704f348309e7b66189e604cf5e01e
47-
GTMSessionFetcher: cea130bbfe5a7edc8d06d3f0d17288c32ffe9925
38+
AppAuth: 31bcec809a638d7bd2f86ea8a52bd45f6e81e7c7
39+
Capacitor: 92088387144015b95e369bd7840e8ef28b8fe9f3
40+
CapacitorCordova: 624ae0d33d61b554eda6823da8ea6c3e5170f646
41+
GoogleSignIn: 1b0c4ec33a6fe282f4fa35d8ac64263230ddaf36
42+
GTMAppAuth: ad5c2b70b9a8689e1a04033c9369c4915bfcbe89
43+
GTMSessionFetcher: 36689134877faeb055b27dfa4ccc9ceaa42e029e
4844

49-
PODFILE CHECKSUM: 26646079fafcf11562258f66ca17008384d1890a
45+
PODFILE CHECKSUM: 0f3f70de2469d6ef87a4f55c3a96868777b79b2e
5046

51-
COCOAPODS: 1.8.4
47+
COCOAPODS: 1.10.1

0 commit comments

Comments
 (0)