diff --git a/README.md b/README.md index 3e3da0c..4c12a0c 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,11 @@ Start by importing `GithubKit` and configure it with your app credentials: ```swift import GithubKit -Github.configure(accessGroup: , - clientID: , - clientSecret: , - redirectURL: URL(string: )!) +Github.configure( + clientID: , + clientSecret: , + redirectURL: URL(string: )! +) ``` ### Auth diff --git a/Sources/Auth/AuthView.swift b/Sources/Auth/AuthView.swift index 3120c37..baf6ff5 100644 --- a/Sources/Auth/AuthView.swift +++ b/Sources/Auth/AuthView.swift @@ -43,7 +43,13 @@ public extension View { @ViewBuilder func githubAuthSheet(isPresented: Binding, _ completion: @escaping Github.Completion) -> some View { if let github = Github.shared { - self.modifier(GithubAuthView(isPresented: isPresented, github: github, completion)) + if let config = github.persistenceManager.readConfiguration() { + self.onAppear { + completion(.success(config)) + } + } else { + self.modifier(GithubAuthView(isPresented: isPresented, github: github, completion)) + } } else { fatalError("Missing call to Github.configure(_:)\n please make sure you set up the library correctly.") } diff --git a/Sources/Auth/Authview+ViewModel.swift b/Sources/Auth/Authview+ViewModel.swift index c5ba48a..0912b9a 100644 --- a/Sources/Auth/Authview+ViewModel.swift +++ b/Sources/Auth/Authview+ViewModel.swift @@ -12,6 +12,7 @@ import SwiftUI extension GithubAuthView { class ViewModel: ObservableObject { + private let github: Github private let completion: Github.Completion let webView: WKWebView let webViewStore: WebViewStore @@ -30,6 +31,7 @@ extension GithubAuthView { private let requestBuilder: Github.Configuration.RequestBuilder init(github: Github, completion: @escaping Github.Completion) { + self.github = github self.completion = completion let webView = WKWebView() self.webView = webView @@ -69,7 +71,9 @@ extension GithubAuthView { isBusy = true Task { do { - completion(.success(try await requestBuilder.getOAuthToken(fromAccessCode: code))) + let token = try await requestBuilder.getOAuthToken(fromAccessCode: code) + github.persistenceManager.persistConfiguration(token) + completion(.success(token)) } catch { completion(.failure(error)) } diff --git a/Sources/Core/Github.swift b/Sources/Core/Github.swift index 63769a5..6aef389 100644 --- a/Sources/Core/Github.swift +++ b/Sources/Core/Github.swift @@ -15,7 +15,6 @@ public struct Github { static private(set) var shared: Github? public static func configure( - accessGroup: String, clientID: String, clientSecret: String, baseURL: URL = URL(string: "https://github.com/login/oauth")!, @@ -28,7 +27,7 @@ public struct Github { baseURL: baseURL, redirectURL: redirectURL ), - persistenceManager: .init(accessGroup: accessGroup) + persistenceManager: .init() ) } } diff --git a/Sources/Core/Persistence/PersistenceManager.swift b/Sources/Core/Persistence/PersistenceManager.swift index a296bbc..2e00334 100644 --- a/Sources/Core/Persistence/PersistenceManager.swift +++ b/Sources/Core/Persistence/PersistenceManager.swift @@ -15,10 +15,8 @@ class PersistenceManager { private static let key: String = "config" - init(accessGroup: String) { - let keychain: KeychainSwift = .init() - keychain.accessGroup = accessGroup - self.keychain = keychain + init() { + self.keychain = .init() self.jsonCoder = .init() } @@ -29,6 +27,6 @@ class PersistenceManager { func persistConfiguration(_ config: Github.Configuration) { guard let data = try? jsonCoder.encode(value: config) else { return } - keychain.set(data, forKey: Self.key) + keychain.set(data, forKey: Self.key, withAccess: .accessibleWhenUnlocked) } }