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
@@ -16,30 +16,24 @@ public class GoogleAuth: CAPPlugin {
1616 guard let path = Bundle . main. path ( forResource: " GoogleService-Info " , ofType: " plist " ) else { return }
1717 guard let dict = NSDictionary ( contentsOfFile: path) as? [ String : AnyObject ] else { return }
1818 guard let clientId = dict [ " CLIENT_ID " ] as? String else { return }
19-
2019 googleSignIn. clientID = clientId;
2120 googleSignIn. delegate = self ;
2221 googleSignIn. presentingViewController = bridge. viewController;
23-
2422 if let serverClientId = getConfigValue ( " serverClientId " ) as? String {
2523 googleSignIn. serverClientID = serverClientId;
2624 }
27-
2825 if let scopes = getConfigValue ( " scopes " ) as? [ String ] {
2926 googleSignIn. scopes = scopes;
3027 }
31-
3228 if let forceAuthCodeConfig = getConfigValue ( " forceCodeForRefreshToken " ) as? Bool {
3329 forceAuthCode = forceAuthCodeConfig;
3430 }
35-
3631 NotificationCenter . default. addObserver ( self , selector: #selector( handleOpenUrl ( _ : ) ) , name: Notification . Name ( CAPNotifications . URLOpen. name ( ) ) , object: nil ) ;
3732 }
38-
33+
3934 @objc
4035 func signIn( _ call: CAPPluginCall ) {
4136 signInCall = call;
42-
4337 DispatchQueue . main. async {
4438 if self . googleSignIn. hasPreviousSignIn ( ) && !self . forceAuthCode {
4539 self . googleSignIn. restorePreviousSignIn ( ) ;
@@ -48,21 +42,19 @@ public class GoogleAuth: CAPPlugin {
4842 }
4943 }
5044 }
51-
45+
5246 @objc
5347 func refresh( _ call: CAPPluginCall ) {
5448 DispatchQueue . main. async {
5549 if self . googleSignIn. currentUser == nil {
5650 call. error ( " User not logged in. " ) ;
5751 return
5852 }
59-
6053 self . googleSignIn. currentUser. authentication. getTokensWithHandler { ( authentication, error) in
6154 guard let authentication = authentication else {
6255 call. error ( error? . localizedDescription ?? " Something went wrong. " ) ;
6356 return ;
6457 }
65-
6658 let authenticationData : [ String : Any ] = [
6759 " accessToken " : authentication. accessToken,
6860 " idToken " : authentication. idToken,
@@ -72,30 +64,28 @@ public class GoogleAuth: CAPPlugin {
7264 }
7365 }
7466 }
75-
67+
7668 @objc
7769 func signOut( _ call: CAPPluginCall ) {
7870 DispatchQueue . main. async {
7971 self . googleSignIn. signOut ( ) ;
8072 }
8173 call. success ( ) ;
8274 }
83-
75+
8476 @objc
8577 func handleOpenUrl( _ notification: Notification ) {
8678 guard let object = notification. object as? [ String : Any ] else {
8779 print ( " There is no object on handleOpenUrl " ) ;
8880 return ;
8981 }
90-
9182 guard let url = object [ " url " ] as? URL else {
9283 print ( " There is no url on handleOpenUrl " ) ;
9384 return ;
9485 }
95-
9686 googleSignIn. handle ( url) ;
9787 }
98-
88+
9989 func processCallback( user: GIDGoogleUser ) {
10090 var userData : [ String : Any ] = [
10191 " authentication " : [
@@ -110,22 +100,19 @@ public class GoogleAuth: CAPPlugin {
110100 " id " : user. userID,
111101 " name " : user. profile. name
112102 ] ;
113-
114103 if let imageUrl = user. profile. imageURL ( withDimension: 100 ) ? . absoluteString {
115104 userData [ " imageUrl " ] = imageUrl;
116105 }
117-
118106 signInCall? . success ( userData) ;
119107 }
120108}
121-
109+
122110extension GoogleAuth : GIDSignInDelegate {
123111 public func sign( _ signIn: GIDSignIn ! , didSignInFor user: GIDGoogleUser ! , withError error: Error ! ) {
124112 if let error = error {
125113 signInCall? . error ( error. localizedDescription) ;
126114 return ;
127115 }
128-
129116 processCallback ( user: user) ;
130117 }
131- }
118+ }
0 commit comments