@@ -8,34 +8,36 @@ import GoogleSignIn
88 */
99@objc ( GoogleAuth)
1010public 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- }
0 commit comments