mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Handle thrown Keychain errors with informational alerts
This commit is contained in:
parent
2d965772e7
commit
cfe1613621
@ -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: ""
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user