Skip to content

Commit

Permalink
0.19 account reauth (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBAndrews authored Dec 4, 2023
1 parent 71a9a9b commit e5dbbda
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
10 changes: 8 additions & 2 deletions Mlem/API/Models/APIErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ private let possibleCredentialErrors = [
"couldnt_find_that_username_or_email"
]

private let possibleAuthenticationErrors = [
"incorrect_password",
"password_incorrect",
"incorrect_login",
"not_logged_in"
]

private let possible2FAErrors = [
"missing_totp_token",
"incorrect_totp_token"
Expand All @@ -29,9 +36,8 @@ private let registrationErrors = [
]

extension APIErrorResponse {
var isIncorrectLogin: Bool { possibleCredentialErrors.contains(error) }
var requires2FA: Bool { possible2FAErrors.contains(error) }
var isNotLoggedIn: Bool { error == "not_logged_in" }
var isNotLoggedIn: Bool { possibleAuthenticationErrors.contains(error) }
var userRegistrationPending: Bool { registrationErrors.contains(error) }
var emailNotVerified: Bool { registrationErrors.contains(error) }
var instanceIsPrivate: Bool { error == "instance_is_private" }
Expand Down
14 changes: 10 additions & 4 deletions Mlem/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct ContentView: View {
@GestureState private var isDetectingLongPress = false

@State private var isPresentingAccountSwitcher: Bool = false
@State private var tokenRefreshAccount: SavedAccount?

@AppStorage("showInboxUnreadBadge") var showInboxUnreadBadge: Bool = true
@AppStorage("homeButtonExists") var homeButtonExists: Bool = false
Expand Down Expand Up @@ -97,10 +98,15 @@ struct ContentView: View {
accountChanged()
}
.onReceive(errorHandler.$sessionExpired) { expired in
if expired, let account = appState.currentActiveAccount {
NotificationDisplayer.presentTokenRefreshFlow(for: account) { updatedAccount in
appState.setActiveAccount(updatedAccount)
}
if expired {
tokenRefreshAccount = appState.currentActiveAccount
}
}
.sheet(item: $tokenRefreshAccount) {
errorHandler.clearExpiredSession()
} content: { account in
TokenRefreshView(account: account) { updatedAccount in
appState.setActiveAccount(updatedAccount)
}
}
.alert(using: $errorAlert) { content in
Expand Down
9 changes: 6 additions & 3 deletions Mlem/Error Handling/ErrorHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,19 @@ class ErrorHandler: ObservableObject {
Task { @MainActor in

if let clientError = error.underlyingError.base as? APIClientError {

if case .invalidSession = clientError {
sessionExpired = true
return
}

if error.title != nil && !InternetConnectionManager.isConnectedToNetwork() {
if error.title != nil, !InternetConnectionManager.isConnectedToNetwork() {
if showNoInternet {
await notifier.add(.noInternet)
}
return
}

if case .response(let apiError, _) = clientError {
if case let .response(apiError, _) = clientError {
await notifier.add(.failure(apiError.error))
return
}
Expand All @@ -68,6 +67,10 @@ class ErrorHandler: ObservableObject {
}
}

func clearExpiredSession() {
sessionExpired = false
}

private func log(_ error: ContextualError, _ file: StaticString, _ function: StaticString, _ line: Int) {
print("☠️ ERROR ☠️")
print("🕵️ -> \(error.underlyingError.description)")
Expand Down
5 changes: 2 additions & 3 deletions Mlem/Views/Shared/Accounts/Add Account View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ struct AddSavedInstanceView: View {
message = badCredentialsMessage
case APIClientError.networking:
message = "Please check your internet connection and try again"
case APIClientError.invalidSession:
message = badCredentialsMessage
case let APIClientError.response(errorResponse, _) where errorResponse.requires2FA:
message = ""

Expand All @@ -387,9 +389,6 @@ struct AddSavedInstanceView: View {
}

return
case let APIClientError.response(errorResponse, _) where errorResponse.isIncorrectLogin:
message = badCredentialsMessage

case let APIClientError.response(errorResponse, _) where errorResponse.emailNotVerified:
message = registrationError

Expand Down
3 changes: 1 addition & 2 deletions Mlem/Views/Shared/TokenRefreshView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ struct TokenRefreshView: View {
} catch {
HapticManager.shared.play(haptic: .failure, priority: .high)

if case let APIClientError.response(apiError, _) = error,
apiError.isIncorrectLogin {
if case APIClientError.invalidSession = error {
updateViewState(.incorrectLogin)
selectedField = .password
return
Expand Down

0 comments on commit e5dbbda

Please sign in to comment.