diff --git a/MastodonKit/Sources/MastodonKit/MastodonClient+Convenience.swift b/MastodonKit/Sources/MastodonKit/MastodonClient+Convenience.swift index dcf046e..0e15c74 100644 --- a/MastodonKit/Sources/MastodonKit/MastodonClient+Convenience.swift +++ b/MastodonKit/Sources/MastodonKit/MastodonClient+Convenience.swift @@ -72,7 +72,8 @@ public extension MastodonClient { ) oauthClient?.authorizeURLHandler = ASWebAuthenticationURLHandler(callbackUrlScheme: callbackUrlScheme, - presentationContextProvider: presentationContextProvider) + presentationContextProvider: presentationContextProvider, + prefersEphemeralWebBrowserSession: true) return try await withCheckedThrowingContinuation { [weak self] continuation in self?.oAuthContinuation = continuation diff --git a/Vernissage/Services/AuthorizationService.swift b/Vernissage/Services/AuthorizationService.swift index cc0a1d3..3573557 100644 --- a/Vernissage/Services/AuthorizationService.swift +++ b/Vernissage/Services/AuthorizationService.swift @@ -14,9 +14,7 @@ public class AuthorizationService { private init() { } /// Access token verification. - public func verifyAccount(session: AuthorizationSession, _ result: @escaping (AccountData?) -> Void) async { - let currentAccount = AccountDataHandler.shared.getCurrentAccountData() - + public func verifyAccount(session: AuthorizationSession, currentAccount: AccountData?, _ result: @escaping (AccountData?) -> Void) async { // When we dont have even one account stored in database then we have to ask user to enter server and sign in. guard let currentAccount, let accessToken = currentAccount.accessToken else { result(nil) @@ -133,8 +131,16 @@ public class AuthorizationService { group.addTask { do { try await self.refreshAccessToken(accountData: account) + + #if DEBUG + ToastrService.shared.showSuccess("New access tokens has been retrieved", imageSystemName: "key.fill") + #endif } catch { - ErrorService.shared.handle(error, message: "Error during refreshing access token for account '\(account.acct)'.") + #if DEBUG + ErrorService.shared.handle(error, message: "Error during refreshing access token for account '\(account.acct)'.", showToastr: true) + #else + ErrorService.shared.handle(error, message: "Error during refreshing access token for account '\(account.acct)'.") + #endif } } } diff --git a/Vernissage/VernissageApp.swift b/Vernissage/VernissageApp.swift index 3f34547..874573e 100644 --- a/Vernissage/VernissageApp.swift +++ b/Vernissage/VernissageApp.swift @@ -65,7 +65,8 @@ struct VernissageApp: App { // Verify access token correctness. let authorizationSession = AuthorizationSession() - await AuthorizationService.shared.verifyAccount(session: authorizationSession) { accountData in + let currentAccount = AccountDataHandler.shared.getCurrentAccountData() + await AuthorizationService.shared.verifyAccount(session: authorizationSession, currentAccount: currentAccount) { accountData in guard let accountData = accountData else { self.applicationViewMode = .signIn return diff --git a/Vernissage/Views/MainView.swift b/Vernissage/Views/MainView.swift index 718684e..e7e5696 100644 --- a/Vernissage/Views/MainView.swift +++ b/Vernissage/Views/MainView.swift @@ -153,15 +153,7 @@ struct MainView: View { Menu { ForEach(self.dbAccounts) { account in Button { - HapticService.shared.touch() - - let accountModel = AccountModel(accountData: account) - self.applicationState.account = accountModel - self.client.setAccount(account: accountModel) - self.applicationState.lastSeenStatusId = account.lastSeenStatusId - self.applicationState.amountOfNewStatuses = 0 - - ApplicationSettingsHandler.shared.setAccountAsDefault(accountData: account) + self.tryToSwitch(account) } label: { if self.applicationState.account?.id == account.id { Label(account.displayName ?? account.acct, systemImage: "checkmark") @@ -232,5 +224,30 @@ struct MainView: View { return "Notifications" } } + + private func tryToSwitch(_ account: AccountData) { + HapticService.shared.touch() + + Task { + // Verify access token correctness. + let authorizationSession = AuthorizationSession() + await AuthorizationService.shared.verifyAccount(session: authorizationSession, currentAccount: account) { accountData in + guard let accountData = accountData else { + ToastrService.shared.showError(subtitle: "Cannot switch accounts.") + return + } + + Task { @MainActor in + let accountModel = AccountModel(accountData: accountData) + self.applicationState.account = accountModel + self.client.setAccount(account: accountModel) + self.applicationState.lastSeenStatusId = account.lastSeenStatusId + self.applicationState.amountOfNewStatuses = 0 + + ApplicationSettingsHandler.shared.setAccountAsDefault(accountData: accountData) + } + } + } + } }