Skip to content

Commit

Permalink
fix: Correctly disconnect user on failed refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Apr 25, 2024
1 parent cf196cb commit 8fadb33
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions MailCore/API/SyncedAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,33 @@ final class SyncedAuthenticator: OAuthAuthenticator {
@LazyInjectService var networkLoginService: InfomaniakNetworkLoginable

func handleFailedRefreshingToken(oldToken: ApiToken, error: Error?) -> Result<OAuthAuthenticator.Credential, Error> {
guard let error = error as NSError?,
error.domain == "invalid_grant" else {
guard let error else {
// Couldn't refresh the token, keep the old token and fetch it later. Maybe because of bad network ?
SentrySDK
.addBreadcrumb(oldToken.generateBreadcrumb(level: .error,
message: "Refreshing token failed - Other \(error.debugDescription)"))
return .failure(error ?? MailError.unknownError)
return .failure(MailError.unknownError)
}

// Couldn't refresh the token, API says it's invalid
SentrySDK.addBreadcrumb(oldToken.generateBreadcrumb(level: .error, message: "Refreshing token failed - Invalid grant"))
refreshTokenDelegate?.didFailRefreshToken(oldToken)
if case .noRefreshToken = (error as? InfomaniakLoginError) {
// Couldn't refresh the token because we don't have a refresh token
SentrySDK
.addBreadcrumb(oldToken.generateBreadcrumb(level: .error,
message: "Refreshing token failed - Cannot refresh infinite token"))
refreshTokenDelegate?.didFailRefreshToken(oldToken)
return .failure(error)
}

if (error as NSError).domain == "invalid_grant" {
// Couldn't refresh the token, API says it's invalid
SentrySDK
.addBreadcrumb(oldToken.generateBreadcrumb(level: .error,
message: "Refreshing token failed - Invalid grant"))
refreshTokenDelegate?.didFailRefreshToken(oldToken)
return .failure(error)
}

// Something else happened
return .failure(error)
}

Expand Down

0 comments on commit 8fadb33

Please sign in to comment.