44![ iOS 18.0+] ( https://img.shields.io/badge/iOS-18.0%2B-crimson.svg )
55![ macOS 15.0+] ( https://img.shields.io/badge/macOS-15.0%2B-skyblue.svg )
66![ tvOS 18.0+] ( https://img.shields.io/badge/tvOS-18.0%2B-blue.svg )
7- ![ visionOS 2.0+] ( https://img.shields.io/badge/visionOS-2.0%2B-magenta.svg )
7+ ![ visionOS 2.0+] ( https://img.shields.io/badge/visionOS-2.0%2B-violet.svg )
8+ ![ watchOS 11.0+] ( https://img.shields.io/badge/watchOS-11.0%2B-magenta.svg )
89[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-indigo.svg )] ( https://opensource.org/licenses/MIT )
910![ Code Coverage] ( https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/codefiesta/87655b6e3c89b9198287b2fefbfa641f/raw/oauthkit-coverage.json )
1011
@@ -16,7 +17,7 @@ OAuthKit is a contemporary, event-driven Swift Package that utilizes the [Observ
1617
1718## OAuthKit Usage
1819
19- The following is an example of the simplest usage of using OAuthKit in macOS:
20+ The following is an example of the simplest usage of using OAuthKit across multiple platforms (iOS, macOS, visionOS, tvOS, watchOS) :
2021
2122``` swift
2223import OAuthKit
@@ -36,19 +37,23 @@ struct OAuthApp: App {
3637 }
3738 .environment (\.oauth , oauth)
3839
40+ #if canImport (WebKit )
3941 WindowGroup (id : " oauth" ) {
4042 OAWebView (oauth : oauth)
4143 }
44+ #endif
4245 }
4346}
4447
4548struct ContentView : View {
4649
50+ #if canImport (WebKit )
4751 @Environment (\.openWindow )
4852 var openWindow
4953
5054 @Environment (\.dismissWindow )
5155 private var dismissWindow
56+ #endif
5257
5358 @Environment (\.oauth )
5459 var oauth: OAuth
@@ -70,7 +75,8 @@ struct ContentView: View {
7075 }
7176 case .receivedDeviceCode (_ , let deviceCode):
7277 Text (" To login, visit" )
73- Text (deviceCode.verificationUri ).foregroundStyle (.blue )
78+ Text (.init (" [\( deviceCode.verificationUri ) ](\( deviceCode.verificationUri ) )" ))
79+ .foregroundStyle (.blue )
7480 Text (" and enter the following code:" )
7581 Text (deviceCode.userCode )
7682 .padding ()
@@ -87,11 +93,24 @@ struct ContentView: View {
8793 var providerList: some View {
8894 List (oauth.providers ) { provider in
8995 Button (provider.id ) {
90- // Start the default PKCE flow (.pkce)
91- oauth.authorize (provider : provider)
96+ authorize (provider : provider)
9297 }
9398 }
9499 }
100+
101+ /// Starts the authorization process for the specified provider.
102+ /// - Parameter provider: the provider to begin authorization for
103+ private func authorize (provider : OAuth.Provider) {
104+ #if canImport (WebKit )
105+ // Use the PKCE grantType for iOS, macOS, visionOS
106+ let grantType: OAuth.GrantType = .pkce (.init ())
107+ #else
108+ // Use the Device Code grantType for tvOS, watchOS
109+ let grantType: OAuth.GrantType = .deviceCode
110+ #endif
111+ // Start the authorization flow
112+ oauth.authorize (provider : provider, grantType : grantType)
113+ }
95114
96115 /// Reacts to oauth state changes by opening or closing authorization windows.
97116 /// - Parameter state: the published state change
@@ -100,9 +119,13 @@ struct ContentView: View {
100119 case .empty , .requestingAccessToken , .requestingDeviceCode :
101120 break
102121 case .authorizing , .receivedDeviceCode :
122+ #if canImport (WebKit )
103123 openWindow (id : " oauth" )
124+ #endif
104125 case .authorized (_ , _ ):
126+ #if canImport (WebKit )
105127 dismissWindow (id : " oauth" )
128+ #endif
106129 }
107130 }
108131}
0 commit comments