Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor: init migration to GoogleSignIn v6
  • Loading branch information
reslear committed Aug 7, 2021
commit 40d337650c781303d7acca1c27134b491d53db44
51 changes: 41 additions & 10 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,65 @@ import Foundation
import Capacitor
import GoogleSignIn

public struct initConfig {
var clientID: String?
var serverClientID: String?
var forceAuthCode: Bool?
var scopes: [String]?
var viewController: UIViewController = nil
}

/**
* Please read the Capacitor iOS Plugin Development Guide
* here: https://capacitor.ionicframework.com/docs/plugins/ios
*/
@objc(GoogleAuth)
public class GoogleAuth: CAPPlugin {
var signInCall: CAPPluginCall?
let googleSignIn: GIDSignIn = GIDSignIn.sharedInstance();
let googleSignIn = GIDSignIn.sharedInstance;
var forceAuthCode: Bool = false;

var signInConfig: GIDConfiguration
var config = initConfig()


public override func load() {
guard let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") else {
print("GoogleService-Info.plist not found");
return;
}
guard let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] else {return}

guard let clientId = dict["CLIENT_ID"] as? String else {return}
googleSignIn.clientID = clientId;
googleSignIn.delegate = self;
googleSignIn.presentingViewController = bridge?.viewController;

config.clientID = clientId

if let serverClientId = getConfigValue("serverClientId") as? String {
googleSignIn.serverClientID = serverClientId;
config.serverClientID = serverClientId;
}

if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
config.forceAuthCode = forceAuthCodeConfig;
}

if let scopes = getConfigValue("scopes") as? [String] {
googleSignIn.scopes = scopes;
config.scopes = scopes;
}
if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool {
forceAuthCode = forceAuthCodeConfig;

if let viewController = bridge?.viewController {
config.viewController = viewController
}
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(CAPNotifications.URLOpen.name()), object: nil);


signInConfig = GIDConfiguration.init(
clientID: clientId,
serverClientID: config.serverClientID
// TODO: scopes: config.scopes https://github.com/google/GoogleSignIn-iOS/pull/30
)

// googleSignIn.delegate = self;

NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name.capacitorOpenURL, object: nil);
}

@objc
Expand All @@ -41,7 +70,9 @@ public class GoogleAuth: CAPPlugin {
if self.googleSignIn.hasPreviousSignIn() && !self.forceAuthCode {
self.googleSignIn.restorePreviousSignIn();
} else {
self.googleSignIn.signIn();
self.googleSignIn.signIn(
with: self.signInConfig,
presenting: self.config.viewController);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ target 'Plugin' do
pod 'Capacitor', :path => '../node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../node_modules/@capacitor/ios'

pod 'GoogleSignIn', '~> 5.0.2'
pod 'GoogleSignIn', '~> 6.0.1'

end

Expand Down