From 3846c160d5cf756e55c1bd48cd84dfd4c75e9a41 Mon Sep 17 00:00:00 2001 From: Victor Bodinaud Date: Sun, 14 Jan 2024 19:31:52 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=EF=B8=8F=20Added=20bearer=20token?= =?UTF-8?q?=20authentication?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Hermes/Hermes.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/Hermes/Hermes.swift b/Sources/Hermes/Hermes.swift index eb99ccc..469c94b 100644 --- a/Sources/Hermes/Hermes.swift +++ b/Sources/Hermes/Hermes.swift @@ -62,6 +62,18 @@ public struct Hermes { public init() { } + private var defaultHeaders: [String: String] { + var headers = ["Content-Type": "application/json"] + let defaults = UserDefaults.standard + guard let token = defaults.string(forKey: "authToken") else { + return headers + } + + headers["Authorization"] = "Bearer \(token)" + + return headers + } + public func load(_ resource: Resource) async throws -> T { var request = URLRequest(url: resource.url) @@ -85,7 +97,7 @@ public struct Hermes { } let configuration = URLSessionConfiguration.default - configuration.httpAdditionalHeaders = ["Content-Type": "application/json"] + configuration.httpAdditionalHeaders = defaultHeaders let session = URLSession(configuration: configuration) let (data, response) = try await session.data(for: request)