From 5fd6bf65f38e1783a9d635e801b9cbddfe795405 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Mon, 25 May 2020 13:42:56 +0300 Subject: [PATCH] Some google auth fixes --- .../Controllers/GoogleSignInController.swift | 5 ++++- AutoCat/Controllers/SettingsController.swift | 21 ++++++++++++++++--- AutoCat/ThirdParty/Api.swift | 4 ++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/AutoCat/Controllers/GoogleSignInController.swift b/AutoCat/Controllers/GoogleSignInController.swift index 3607130..daec371 100644 --- a/AutoCat/Controllers/GoogleSignInController.swift +++ b/AutoCat/Controllers/GoogleSignInController.swift @@ -15,6 +15,7 @@ class GoogleSignInController: UIViewController, WKNavigationDelegate { @IBOutlet weak var webView: WKWebView! private var codeVerifier: String = "" + public var completion: (() -> Void)? override func viewDidLoad() { super.viewDidLoad() @@ -60,7 +61,9 @@ class GoogleSignInController: UIViewController, WKNavigationDelegate { Settings.shared.user.googleIdToken = idToken Settings.shared.user.googleRefreshToken = refreshToken DispatchQueue.main.async { - self.dismiss(animated: true, completion: nil) + self.dismiss(animated: true) { + self.completion?() + } } } } diff --git a/AutoCat/Controllers/SettingsController.swift b/AutoCat/Controllers/SettingsController.swift index 95d0689..d280c86 100644 --- a/AutoCat/Controllers/SettingsController.swift +++ b/AutoCat/Controllers/SettingsController.swift @@ -43,11 +43,16 @@ class SettingsController: FormViewController { func displayLogoutSheet() { guard let jwtString = Settings.shared.user.googleIdToken, let jwt = JWT(string: jwtString) else { return } - let sheet = UIAlertController(title: jwt.payload.name, message: "You are currently signed in with email \(jwt.payload.email). It allows us to find more data about vehicles.", preferredStyle: .actionSheet) + let sheet = UIAlertController(title: jwt.payload.name, message: "You are currently signed in with email \(jwt.payload.email). It will help to gather more data about vehicles.", preferredStyle: .actionSheet) let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in sheet.dismiss(animated: true, completion: nil) } let logout = UIAlertAction(title: "Sign Out", style: .destructive) { _ in Settings.shared.user.googleIdToken = nil + Settings.shared.user.googleRefreshToken = nil + if let googleRow = self.form.rowBy(tag: "GoogleAccount") as? LabelRow { + googleRow.value = "Log In" + googleRow.reload() + } } sheet.addAction(logout) @@ -57,7 +62,17 @@ class SettingsController: FormViewController { func loginToGoogle() { let storyboard = UIStoryboard(name: "Main", bundle: nil) - let vc = storyboard.instantiateViewController(identifier: "GoogleSignInController") - self.present(vc, animated: true) + if let vc = storyboard.instantiateViewController(identifier: "GoogleSignInController") as? GoogleSignInController { + vc.completion = { + guard let googleRow = self.form.rowBy(tag: "GoogleAccount") as? LabelRow else { return } + if let jwtString = Settings.shared.user.googleIdToken, let jwt = JWT(string: jwtString) { + googleRow.value = jwt.payload.email + } else { + googleRow.value = "Log In" + } + googleRow.reload() + } + self.present(vc, animated: true) + } } } diff --git a/AutoCat/ThirdParty/Api.swift b/AutoCat/ThirdParty/Api.swift index 3a3f765..fb25795 100644 --- a/AutoCat/ThirdParty/Api.swift +++ b/AutoCat/ThirdParty/Api.swift @@ -72,6 +72,10 @@ class Api { } public static func checkVehicle(by number: String) -> Observable { + if let token = Settings.shared.user.googleIdToken, let jwt = JWT(string: token) { + + } + return self.makeRequest(api: "vehicles/check", method: "POST", body: ["number": number]).map { (vehicle: Vehicle) -> Vehicle in vehicle.addedDate = Date().timeIntervalSince1970*1000 return vehicle