@@ -10,7 +10,7 @@ import SwiftUI
1010
1111struct ContentView : View {
1212
13- #if !os(tvOS )
13+ #if canImport(WebKit )
1414 @Environment ( \. openWindow)
1515 var openWindow
1616
@@ -46,7 +46,8 @@ struct ContentView: View {
4646 }
4747 case . receivedDeviceCode( _, let deviceCode) :
4848 Text ( " To login, visit " )
49- Text ( deviceCode. verificationUri) . foregroundStyle ( . blue)
49+ Text ( . init( " [ \( deviceCode. verificationUri) ]( \( deviceCode. verificationUri) ) " ) )
50+ . foregroundStyle ( . blue)
5051 Text ( " and enter the following code: " )
5152 Text ( deviceCode. userCode)
5253 . padding ( )
@@ -63,13 +64,25 @@ struct ContentView: View {
6364 var providerList : some View {
6465 List ( oauth. providers) { provider in
6566 Button ( provider. id) {
66- // Start the authorization flow (use .deviceCode for tvOS)
67- let grantType : OAuth . GrantType = . pkce( . init( ) )
68- oauth. authorize ( provider: provider, grantType: grantType)
67+ authorize ( provider: provider)
6968 }
7069 }
7170 }
72-
71+
72+ /// Starts the authorization process for the specified provider.
73+ /// - Parameter provider: the provider to begin authorization for
74+ private func authorize( provider: OAuth . Provider ) {
75+ #if canImport(WebKit)
76+ // Use the PKCE grantType for iOS, macOS, visionOS
77+ let grantType : OAuth . GrantType = . pkce( . init( ) )
78+ #else
79+ // Use the Device Code grantType for tvOS, watchOS
80+ let grantType : OAuth . GrantType = . deviceCode
81+ #endif
82+ // Start the authorization flow
83+ oauth. authorize ( provider: provider, grantType: grantType)
84+ }
85+
7386 /// Reacts to oauth state changes by opening or closing authorization windows.
7487 /// - Parameter state: the published state change
7588 private func handle( state: OAuth . State ) {
@@ -91,13 +104,13 @@ struct ContentView: View {
91104 }
92105
93106 private func openWebView( ) {
94- #if !os(tvOS )
107+ #if canImport(WebKit )
95108 openWindow ( id: . oauth)
96109 #endif
97110 }
98111
99112 private func dismissWebView( ) {
100- #if !os(tvOS )
113+ #if canImport(WebKit )
101114 dismissWindow ( id: . oauth)
102115 #endif
103116 }
0 commit comments