From 8894b50dd50e203983d5f9d7f2c76db42e3a0779 Mon Sep 17 00:00:00 2001
From: pragatimodi <110490169+pragatimodi@users.noreply.github.com>
Date: Mon, 6 May 2024 12:14:37 -0700
Subject: [PATCH] Add phone auth settings (#12327)
---
.../SwiftApplication.plist | 5 +-
.../SettingsViewController.swift | 96 +++++++++++++++++--
2 files changed, 90 insertions(+), 11 deletions(-)
diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist
index 50378fe58b0..d1baff15f24 100644
--- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist
+++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/SwiftApplication.plist
@@ -24,7 +24,10 @@
CFBundleURLName
CFBundleURLSchemes
-
+
+ app-1-1085102361755-ios-2b9d4a82e5df229f
+ app-1-1085102361755-ios-74df231d8a2072e45dca3e
+
CFBundleVersion
diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift
index fc6b91e9a23..8757386f24d 100644
--- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift
+++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/SettingsViewController.swift
@@ -22,6 +22,8 @@ enum SettingsAction: String {
case toggleSecureTokenAPI = "Secure Token"
case toggleActiveApp = "Active App"
case toggleAccessGroup = "Current Access Group"
+ case toggleAPNSToken = "APNs Token"
+ case toggleAppCredential = "App Credential"
case setAuthLanugage = "Auth Language"
case useAppLanguage = "Use App Language"
case togglePhoneAppVerification = "Disable App Verification (Phone)"
@@ -79,6 +81,10 @@ class SettingsViewController: UIViewController, DataSourceProviderDelegate {
setAuthLanguage()
case .useAppLanguage:
auth.useAppLanguage()
+ case .toggleAPNSToken:
+ clearAPNSToken()
+ case .toggleAppCredential:
+ clearAppCredential()
case .togglePhoneAppVerification:
guard let settings = auth.settings else {
fatalError("Unset auth.settings")
@@ -118,6 +124,37 @@ class SettingsViewController: UIViewController, DataSourceProviderDelegate {
}
}
+ 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)"
+
+ let prompt = UIAlertController(title: nil, message: "Clear APNs Token?",
+ preferredStyle: .alert)
+ let okAction = UIAlertAction(title: "OK", style: .default) { action in
+ AppManager.shared.auth().tokenManager.token = nil
+ self.updateUI()
+ }
+ prompt.addAction(okAction)
+ present(prompt, animated: true)
+ }
+
+ func clearAppCredential() {
+ if let credential = AppManager.shared.auth().appCredentialManager.credential {
+ let prompt = UIAlertController(title: nil, message: "Clear APNs Token?",
+ preferredStyle: .alert)
+ let okAction = UIAlertAction(title: "OK", style: .default) { action in
+ AppManager.shared.auth().appCredentialManager.clearCredential()
+ self.updateUI()
+ }
+ prompt.addAction(okAction)
+ present(prompt, animated: true)
+ }
+ }
+
private func setAuthLanguage() {
let prompt = UIAlertController(title: nil, message: "Enter Language Code For Auth:",
preferredStyle: .alert)
@@ -160,6 +197,24 @@ class SettingsViewController: UIViewController, DataSourceProviderDelegate {
options: .transitionCrossDissolve,
animations: { tableView.reloadData() })
}
+
+ func showPromptWithTitle(_ title: String, message: String, showCancelButton: Bool,
+ completion: @escaping (Bool, String?) -> 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)
+ }
}
// MARK: - Extending a `AuthSettings` to conform to `DataSourceProvidable`
@@ -187,21 +242,42 @@ extension AuthSettings: DataSourceProvidable {
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 ..< midIndex])...\(string[endIndex...])"
+ }
+
// 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", 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 ? "Production" : "Sandbox"
+ return "\(truncatedToken)(\(tokenType))"
+ }
+
+ 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)
}
private var languageSection: Section {