Skip to content

Commit

Permalink
Add PhoneAuth details in Settings view
Browse files Browse the repository at this point in the history
  • Loading branch information
pragatimodi committed Jan 29, 2024
1 parent 448b598 commit afc4af6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array/>
<array>
<string>com.googleusercontent.apps.585304629422-ab795evno6vsrj7i7o1a3gi6eo01508b</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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..<midIndex])...\(string[endIndex...])"
}

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)

// 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
Expand Down

0 comments on commit afc4af6

Please sign in to comment.