From afc4af669f12e77d11ea59a2b7a4c158ba998b1a Mon Sep 17 00:00:00 2001 From: Pragati Date: Mon, 29 Jan 2024 15:38:38 +0530 Subject: [PATCH] Add PhoneAuth details in Settings view --- .../SwiftApplication.plist | 4 +- .../SettingsViewController.swift | 93 +++++++++++++++++-- 2 files changed, 86 insertions(+), 11 deletions(-) diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist index 50378fe58b0..cdbcd12729a 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist @@ -24,7 +24,9 @@ CFBundleURLName CFBundleURLSchemes - + + com.googleusercontent.apps.585304629422-ab795evno6vsrj7i7o1a3gi6eo01508b + CFBundleVersion diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift index fc6b91e9a23..a6e1421aa38 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift @@ -186,23 +186,96 @@ extension AuthSettings: DataSourceProvidable { detailTitle: "Current Access Group")] return Section(headerDescription: "Keychain Access Groups", items: items) } + + func truncatedString(string: String, length: Int) -> String { + guard string.count > length else { return string } + + let half = (length - 3) / 2 + let startIndex = string.startIndex + let midIndex = string.index(startIndex, offsetBy: half) // Ensure correct mid index + let endIndex = string.index(startIndex, offsetBy: string.count - half) + + return "\(string[startIndex.. Void) { + let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) + + alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in + let userInput = alertController.textFields?.first?.text + completion(true, userInput) + })) + + if showCancelButton { + alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in + completion(false, nil) + })) + } + + alertController.addTextField(configurationHandler: nil) + + // Present the alert controller + // Make sure to present it from a view controller + // For example, if this code is inside a UIViewController, you can use `self.present(alertController, animated: true, completion: nil)` + } + + + // TODO: Add ability to click and clear both of these fields. private var phoneAuthSection: Section { - var tokenString = "No Token" - var credentialString = "No Credential" - if let token = AppManager.shared.auth().tokenManager.token { - let tokenType = token.type == .prod ? "Production" : "Sandbox" - tokenString = "token: \(token.string): type: \(tokenType)" + let items = [Item(title: APNSTokenString(), detailTitle: "APNs Token"), + Item(title: appCredentialString(), detailTitle: "App Credential")] + return Section(headerDescription: "Phone Auth - TODO toggle off", items: items) + } + + func APNSTokenString() -> String { + guard let token = AppManager.shared.auth().tokenManager.token else { + return "No APNs token" + } + + let truncatedToken = truncatedString(string: token.string, length: 19) + let tokenType = token.type == .prod ? "P" : "S" + return "\(truncatedToken)(\(tokenType))" + } + + func clearAPNSToken() { + guard let token = AppManager.shared.auth().tokenManager.token else { + return + } + + let tokenType = token.type == .prod ? "Production" : "Sandbox" + let message = "token: \(token.string)\ntype: \(tokenType)" + + self.showPromptWithTitle("Clear APNs Token?", message: message, showCancelButton: true) { (userPressedOK, userInput) in + if userPressedOK { + AppManager.shared.auth().tokenManager.token = nil + } } + } + + func appCredentialString() -> String { if let credential = AppManager.shared.auth().appCredentialManager.credential { - // TODO: Maybe use truncatedString like ObjC sample - credentialString = "\(credential.receipt)/\(credential.secret ?? "nil")" + let truncatedReceipt = truncatedString(string: credential.receipt, length: 13) + let truncatedSecret = truncatedString(string: credential.secret ?? "", length: 13) + return "\(truncatedReceipt)/\(truncatedSecret)" + } else { + return "No App Credential" } - let items = [Item(title: tokenString, detailTitle: "APNs Token"), - Item(title: credentialString, detailTitle: "App Credential")] - return Section(headerDescription: "Phone Auth - TODO toggle off", items: items) } + + + func clearAppCredential() { + if let credential = AppManager.shared.auth().appCredentialManager.credential { + let message = "receipt: \(credential.receipt)\nsecret: \(credential.secret)" + + showPromptWithTitle("Clear App Credential?", message: message, showCancelButton: true) { (userPressedOK, _) in + if userPressedOK { + AppManager.shared.auth().appCredentialManager.clearCredential() + } + } + } + } + private var languageSection: Section { let languageCode = AppManager.shared.auth().languageCode