11import Foundation
22import Capacitor
33import 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
1010public 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+
117122extension 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