diff --git a/Shared/Account/AccountModel.swift b/Shared/Account/AccountModel.swift index 4dc3aba..1d91b95 100644 --- a/Shared/Account/AccountModel.swift +++ b/Shared/Account/AccountModel.swift @@ -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: "" + ) } } } diff --git a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift index 9000ace..b4d24a6 100644 --- a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift +++ b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift @@ -10,9 +10,16 @@ extension WriteFreelyModel { let user = try result.get() fetchUserCollections() fetchUserPosts() - saveTokenToKeychain(user.token, username: user.username, server: account.server) - DispatchQueue.main.async { - self.account.login(user) + 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 { diff --git a/Shared/Models/WriteFreelyModel.swift b/Shared/Models/WriteFreelyModel.swift index d392e02..e6c9a23 100644 --- a/Shared/Models/WriteFreelyModel.swift +++ b/Shared/Models/WriteFreelyModel.swift @@ -51,18 +51,25 @@ final class WriteFreelyModel: ObservableObject { print("Server URL not found") return } - guard let token = self.fetchTokenFromKeychain( - username: self.account.username, - server: self.account.server - ) else { - print("Could not fetch token from Keychain") - return + do { + guard let token = try self.fetchTokenFromKeychain( + username: self.account.username, + server: self.account.server + ) else { + 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() } }