Handle thrown Keychain errors with informational alerts

This commit is contained in:
Angelo Stavrow 2021-08-20 17:13:41 -04:00
parent 2d965772e7
commit cfe1613621
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
3 changed files with 46 additions and 14 deletions

View File

@ -6,6 +6,9 @@ enum AccountError: Error {
case usernameNotFound
case serverNotFound
case invalidServerURL
case couldNotSaveTokenToKeychain
case couldNotFetchTokenFromKeychain
case couldNotDeleteTokenFromKeychain
}
extension AccountError: LocalizedError {
@ -31,6 +34,21 @@ extension AccountError: LocalizedError {
"Please enter a valid instance domain name. It should look like \"https://example.com\" or \"write.as\".", // swiftlint:disable:this line_length
comment: ""
)
case .couldNotSaveTokenToKeychain:
return NSLocalizedString(
"There was a problem trying to save your access token to the device, please try logging in again.",
comment: ""
)
case .couldNotFetchTokenFromKeychain:
return NSLocalizedString(
"There was a problem trying to fetch your access token from the device, please try logging in again.",
comment: ""
)
case .couldNotDeleteTokenFromKeychain:
return NSLocalizedString(
"There was a problem trying to delete your access token from the device, please try logging out again.",
comment: ""
)
}
}
}

View File

@ -10,10 +10,17 @@ extension WriteFreelyModel {
let user = try result.get()
fetchUserCollections()
fetchUserPosts()
saveTokenToKeychain(user.token, username: user.username, server: account.server)
do {
try saveTokenToKeychain(user.token, username: user.username, server: account.server)
DispatchQueue.main.async {
self.account.login(user)
}
} catch {
DispatchQueue.main.async {
self.loginErrorMessage = "There was a problem storing your access token to the Keychain."
self.isPresentingLoginErrorAlert = true
}
}
} catch WFError.notFound {
DispatchQueue.main.async {
self.loginErrorMessage = AccountError.usernameNotFound.localizedDescription

View File

@ -51,18 +51,25 @@ final class WriteFreelyModel: ObservableObject {
print("Server URL not found")
return
}
guard let token = self.fetchTokenFromKeychain(
do {
guard let token = try self.fetchTokenFromKeychain(
username: self.account.username,
server: self.account.server
) else {
print("Could not fetch token from Keychain")
self.loginErrorMessage = AccountError.couldNotFetchTokenFromKeychain.localizedDescription
self.isPresentingLoginErrorAlert = true
return
}
self.account.login(WFUser(token: token, username: self.account.username))
self.client = WFClient(for: serverURL)
self.client?.user = self.account.user
self.fetchUserCollections()
self.fetchUserPosts()
} catch {
self.loginErrorMessage = AccountError.couldNotFetchTokenFromKeychain.localizedDescription
self.isPresentingLoginErrorAlert = true
}
}
}