Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add phone auth settings #12327

Merged
merged 9 commits into from
May 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,89 @@ 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...])"
}

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)
}

// 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"
pragatimodi marked this conversation as resolved.
Show resolved Hide resolved
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)"

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 {
let truncatedReceipt = truncatedString(string: credential.receipt, length: 13)
let truncatedSecret = truncatedString(string: credential.secret ?? "", length: 13)
return "\(truncatedReceipt)/\(truncatedSecret)"
} else {
return "No App Credential"
}
}

func clearAppCredential() {
if let credential = AppManager.shared.auth().appCredentialManager.credential {
// TODO: Maybe use truncatedString like ObjC sample
credentialString = "\(credential.receipt)/\(credential.secret ?? "nil")"
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()
}
}
}
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 {
Expand Down
Loading