Skip to content

Commit d6d58b6

Browse files
author
Thibaud
authored
Add support for forceAuthCode on iOS and Android (#46)
* Add support for forceAuthCode on iOS and Android * fix indent * fix build on ios
1 parent 00cbfd5 commit d6d58b6

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ local.properties
5555
.gradle/
5656

5757
# Signing files
58-
.signing/
58+
.signing/
59+
60+
# Android
61+
/android/.idea
62+
/android/android.iml

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ Provide configuration in root `capacitor.config.json`
6363
"plugins": {
6464
"GoogleAuth": {
6565
"scopes": ["profile", "email"],
66-
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"
66+
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
67+
"forceCodeForRefreshToken" : true
6768
}
6869
}
6970
}
7071

7172
```
73+
74+
Note : `forceCodeForRefreshToken` force user to select email address to regenerate AuthCode used to get a valid refreshtoken (work on iOS and Android) (This is used for offline access and serverside handling)

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ public class GoogleAuth extends Plugin {
2727
@Override
2828
public void load() {
2929
String clientId = this.getContext().getString(R.string.server_client_id);
30+
Boolean forceCodeForRefreshToken = false;
31+
32+
Boolean forceRefreshToken = (Boolean) getConfigValue("forceCodeForRefreshToken");
33+
if (forceRefreshToken != null) {
34+
forceCodeForRefreshToken = forceRefreshToken;
35+
}
36+
3037
GoogleSignInOptions.Builder googleSignInBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
31-
.requestIdToken(clientId)
32-
.requestServerAuthCode(clientId)
33-
.requestEmail();
38+
.requestIdToken(clientId)
39+
.requestServerAuthCode(clientId, forceCodeForRefreshToken)
40+
.requestEmail();
3441

3542
try {
3643
JSONArray scopeArray = (JSONArray) getConfigValue("scopes");

demo/capacitor.config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"plugins": {
99
"GoogleAuth": {
1010
"scopes": ["profile", "email"],
11-
"serverClientId": "744254868856-6j8dflcgq0tiqt0b506b61etukcq3rdm.apps.googleusercontent.com"
11+
"serverClientId": "744254868856-6j8dflcgq0tiqt0b506b61etukcq3rdm.apps.googleusercontent.com",
12+
"forceCodeForRefreshToken" : true
1213
}
1314
}
1415
}

ios/Plugin/Plugin.swift

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import Capacitor
33
import GoogleSignIn
4-
4+
55
/**
66
* Please read the Capacitor iOS Plugin Development Guide
77
* here: https://capacitor.ionicframework.com/docs/plugins/ios
@@ -10,54 +10,59 @@ import GoogleSignIn
1010
public class GoogleAuth: CAPPlugin {
1111
var signInCall: CAPPluginCall?
1212
let googleSignIn: GIDSignIn = GIDSignIn.sharedInstance();
13-
13+
var forceAuthCode: Bool = false;
14+
1415
public override func load() {
1516
guard let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") else {return}
1617
guard let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] else {return}
1718
guard let clientId = dict["CLIENT_ID"] as? String else {return}
18-
19+
1920
googleSignIn.clientID = clientId;
2021
googleSignIn.delegate = self;
2122
googleSignIn.presentingViewController = bridge.viewController;
22-
23+
2324
if let serverClientId = getConfigValue("serverClientId") as? String {
2425
googleSignIn.serverClientID = serverClientId;
2526
}
26-
27+
2728
if let scopes = getConfigValue("scopes") as? [String] {
2829
googleSignIn.scopes = scopes;
2930
}
30-
31+
32+
if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
33+
forceAuthCode = forceAuthCodeConfig;
34+
}
35+
3136
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(CAPNotifications.URLOpen.name()), object: nil);
3237
}
33-
38+
3439
@objc
3540
func signIn(_ call: CAPPluginCall) {
3641
signInCall = call;
37-
42+
3843
DispatchQueue.main.async {
39-
if self.googleSignIn.hasPreviousSignIn() {
44+
if self.googleSignIn.hasPreviousSignIn() && !self.forceAuthCode {
4045
self.googleSignIn.restorePreviousSignIn();
4146
} else {
4247
self.googleSignIn.signIn();
4348
}
4449
}
4550
}
46-
51+
4752
@objc
4853
func refresh(_ call: CAPPluginCall) {
4954
DispatchQueue.main.async {
5055
if self.googleSignIn.currentUser == nil {
5156
call.error("User not logged in.");
5257
return
5358
}
54-
59+
5560
self.googleSignIn.currentUser.authentication.getTokensWithHandler { (authentication, error) in
5661
guard let authentication = authentication else {
5762
call.error(error?.localizedDescription ?? "Something went wrong.");
5863
return;
5964
}
60-
65+
6166
let authenticationData: [String: Any] = [
6267
"accessToken": authentication.accessToken,
6368
"idToken": authentication.idToken,
@@ -67,30 +72,30 @@ public class GoogleAuth: CAPPlugin {
6772
}
6873
}
6974
}
70-
75+
7176
@objc
7277
func signOut(_ call: CAPPluginCall) {
7378
DispatchQueue.main.async {
7479
self.googleSignIn.signOut();
7580
}
7681
call.success();
7782
}
78-
83+
7984
@objc
8085
func handleOpenUrl(_ notification: Notification) {
8186
guard let object = notification.object as? [String: Any] else {
8287
print("There is no object on handleOpenUrl");
8388
return;
8489
}
85-
90+
8691
guard let url = object["url"] as? URL else {
8792
print("There is no url on handleOpenUrl");
8893
return;
8994
}
90-
95+
9196
googleSignIn.handle(url);
9297
}
93-
98+
9499
func processCallback(user: GIDGoogleUser) {
95100
var userData: [String: Any] = [
96101
"authentication": [
@@ -105,22 +110,22 @@ public class GoogleAuth: CAPPlugin {
105110
"id": user.userID,
106111
"name": user.profile.name
107112
];
108-
113+
109114
if let imageUrl = user.profile.imageURL(withDimension: 100)?.absoluteString {
110115
userData["imageUrl"] = imageUrl;
111116
}
112-
117+
113118
signInCall?.success(userData);
114119
}
115120
}
116-
121+
117122
extension GoogleAuth: GIDSignInDelegate {
118123
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
119124
if let error = error {
120125
signInCall?.error(error.localizedDescription);
121126
return;
122127
}
123-
128+
124129
processCallback(user: user);
125130
}
126-
}
131+
}

0 commit comments

Comments
 (0)