Skip to content

Commit

Permalink
Merge pull request #6 from sereisoglu/AuthErrorHandle
Browse files Browse the repository at this point in the history
Handling Auth Errors
  • Loading branch information
sereisoglu authored Apr 23, 2024
2 parents 72b1703 + 18965db commit 565465f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ extension Notification.Name {

static let purchase = Notification.Name("purchase")
static let webhooksChange = Notification.Name("webhooksChange")
static let apiServiceRequestError = Notification.Name("apiServiceRequestError")
}
24 changes: 24 additions & 0 deletions Shared/Managers/MeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ final class MeManager: ObservableObject {
analyticsManager.signIn(id: me?.distinctId)
purchaseManager.setKid(id)
isSignedIn = true

setupObservers()
}

private func setupObservers() {
NotificationCenter.default.addObserver(
forName: .apiServiceRequestError,
object: nil,
queue: .main,
using: { [weak self] notification in
guard let self,
let error = notification.object as? RCError else {
return
}

switch error {
case .invalidAuthorizationToken,
.expiredAuthorizationToken:
self.signOut()
default:
return
}
}
)
}

@discardableResult
Expand Down
2 changes: 2 additions & 0 deletions Shared/Network/APIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ final class APIService {
print("Request Error:", endpoint.urlString, String(decoding: data, as: UTF8.self))
let decodedData = try JSONDecoder().decode(RCErrorResponse.self, from: data)
continuation.resume(throwing: RCError(error: decodedData))

NotificationCenter.default.post(name: .apiServiceRequestError, object: RCError(error: decodedData))
} catch {
print("Request Error:", endpoint.urlString, error)
continuation.resume(throwing: RCError.internal(.decodeFailure(error)))
Expand Down

0 comments on commit 565465f

Please sign in to comment.