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 usernameNotFound
|
||||||
case serverNotFound
|
case serverNotFound
|
||||||
case invalidServerURL
|
case invalidServerURL
|
||||||
|
case couldNotSaveTokenToKeychain
|
||||||
|
case couldNotFetchTokenFromKeychain
|
||||||
|
case couldNotDeleteTokenFromKeychain
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AccountError: LocalizedError {
|
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
|
"Please enter a valid instance domain name. It should look like \"https://example.com\" or \"write.as\".", // swiftlint:disable:this line_length
|
||||||
comment: ""
|
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,9 +10,16 @@ extension WriteFreelyModel {
|
|||||||
let user = try result.get()
|
let user = try result.get()
|
||||||
fetchUserCollections()
|
fetchUserCollections()
|
||||||
fetchUserPosts()
|
fetchUserPosts()
|
||||||
saveTokenToKeychain(user.token, username: user.username, server: account.server)
|
do {
|
||||||
DispatchQueue.main.async {
|
try saveTokenToKeychain(user.token, username: user.username, server: account.server)
|
||||||
self.account.login(user)
|
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 {
|
} catch WFError.notFound {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
@ -51,18 +51,25 @@ final class WriteFreelyModel: ObservableObject {
|
|||||||
print("Server URL not found")
|
print("Server URL not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
guard let token = self.fetchTokenFromKeychain(
|
do {
|
||||||
username: self.account.username,
|
guard let token = try self.fetchTokenFromKeychain(
|
||||||
server: self.account.server
|
username: self.account.username,
|
||||||
) else {
|
server: self.account.server
|
||||||
print("Could not fetch token from Keychain")
|
) else {
|
||||||
return
|
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
|
||||||
}
|
}
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user