Skip to content

Commit ce4adc2

Browse files
committed
Add custom scopes #7
1 parent fd07dcb commit ce4adc2

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ Make sure you have `GoogleService-Info.plist` with `CLIENT_ID`
4141

4242
Add `REVERSED_CLIENT_ID` as url scheme to `Info.plist`
4343

44+
### Configure
45+
Provide configuration in root `capacitor.config.json`
46+
```json
47+
{
48+
"plugins": {
49+
"GoogleAuth": {
50+
"scopes": ["profile", "email"]
51+
}
52+
}
53+
}
54+
55+
```
56+
4457
### Support
4558
✔️ iOS
4659

ios/Plugin/Plugin.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,43 @@ import GoogleSignIn
88
*/
99
@objc(GoogleAuth)
1010
public class GoogleAuth: CAPPlugin {
11-
var pluginCall: CAPPluginCall?
11+
var signInCall: CAPPluginCall?
12+
let googleSignIn: GIDSignIn = GIDSignIn.sharedInstance();
1213

1314
public override func load() {
1415
guard let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") else {return}
1516
guard let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] else {return}
1617
guard let clientId = dict["CLIENT_ID"] as? String else {return}
1718

18-
GIDSignIn.sharedInstance().clientID = clientId;
19-
GIDSignIn.sharedInstance().delegate = self;
20-
GIDSignIn.sharedInstance().uiDelegate = self;
19+
googleSignIn.clientID = clientId;
20+
googleSignIn.delegate = self;
21+
googleSignIn.uiDelegate = self;
22+
23+
if let scopes = getConfigValue("scopes") as? [String] {
24+
googleSignIn.scopes = scopes;
25+
}
2126

2227
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(CAPNotifications.URLOpen.name()), object: nil);
2328
}
2429

2530
@objc
2631
func signIn(_ call: CAPPluginCall) {
27-
pluginCall = call;
32+
signInCall = call;
2833

2934
DispatchQueue.main.async {
30-
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
31-
GIDSignIn.sharedInstance().signInSilently();
35+
if self.googleSignIn.hasAuthInKeychain() {
36+
self.googleSignIn.signInSilently();
3237
} else {
33-
GIDSignIn.sharedInstance().signIn();
38+
self.googleSignIn.signIn();
3439
}
3540
}
3641
}
3742

3843
@objc
3944
func signOut(_ call: CAPPluginCall) {
4045
DispatchQueue.main.async {
41-
GIDSignIn.sharedInstance().signOut();
42-
}
46+
self.googleSignIn.signOut();
47+
}
4348
call.success();
4449
}
4550

@@ -61,11 +66,11 @@ public class GoogleAuth: CAPPlugin {
6166
}
6267

6368
let sourceApplication = options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String;
64-
GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: [:]);
69+
googleSignIn.handle(url, sourceApplication: sourceApplication, annotation: [:]);
6570
}
6671

6772
func processCallback(user: GIDGoogleUser) {
68-
pluginCall?.success([
73+
signInCall?.success([
6974
"authentication": [
7075
"accessToken": user.authentication.accessToken,
7176
"idToken": user.authentication.idToken,
@@ -78,7 +83,7 @@ public class GoogleAuth: CAPPlugin {
7883
extension GoogleAuth: GIDSignInDelegate {
7984
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
8085
if let error = error {
81-
pluginCall?.error(error.localizedDescription);
86+
signInCall?.error(error.localizedDescription);
8287
return;
8388
}
8489

src/web.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { WebPlugin } from '@capacitor/core';
22
import { GoogleAuthPlugin } from './definitions';
3+
// @ts-ignore
4+
import config from '../../../../../capacitor.config.json';
35

46
export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
57
constructor() {
@@ -27,6 +29,11 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
2729
const clientConfig: gapi.auth2.ClientConfig = {
2830
client_id: (document.getElementsByName('google-signin-client_id')[0] as any).content
2931
};
32+
33+
if (config.plugins.GoogleAuth != null && config.plugins.GoogleAuth.scopes != null) {
34+
clientConfig.scope = config.plugins.GoogleAuth.scopes.join(' ');
35+
}
36+
3037
gapi.auth2.init(clientConfig);
3138
});
3239
}

0 commit comments

Comments
 (0)