Skip to content

Commit

Permalink
Reschedule refresh if internet is inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
nickkhg committed Sep 18, 2024
1 parent bb0e28f commit dc765c0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/FronteggSwift/FronteggAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ public class FronteggAuth: ObservableObject {
}

public func refreshTokenIfNeeded() async -> Bool {

guard await NetworkStatusMonitor.isActive else {
self.logger.info("Refresh rescheduled due to inactive internet")

scheduleTokenRefresh(offset: 10)
return false
}

guard let refreshToken = self.refreshToken else {
self.logger.info("no refresh token found")
return false
Expand Down
35 changes: 35 additions & 0 deletions Sources/FronteggSwift/utils/NetworkStatusMonitor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// NetworkStatusMonitor.swift
//
//
// Created by Nick Hagi on 18/09/2024.
//

import Foundation
import Network

struct NetworkStatusMonitor {

private static let queue = DispatchQueue(label: "NetworkStatusMonitor")

static var isActive: Bool {
get async {
let monitor = NWPathMonitor()
let result = await withCheckedContinuation { continuation in

monitor.pathUpdateHandler = { path in
switch path.status {
case .satisfied:
continuation.resume(returning: true)
default:
continuation.resume(returning: false)
}
}

monitor.start(queue: queue)
}
monitor.cancel()
return result
}
}
}

0 comments on commit dc765c0

Please sign in to comment.