Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OAuthSample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
repositoryURL = "https://github.com/codefiesta/OAuthKit.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.2.2;
minimumVersion = 1.3.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
29 changes: 21 additions & 8 deletions OAuthSample/Classes/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct ContentView: View {

#if !os(tvOS)
#if canImport(WebKit)
@Environment(\.openWindow)
var openWindow

Expand Down Expand Up @@ -46,7 +46,8 @@ struct ContentView: View {
}
case .receivedDeviceCode(_, let deviceCode):
Text("To login, visit")
Text(deviceCode.verificationUri).foregroundStyle(.blue)
Text(.init("[\(deviceCode.verificationUri)](\(deviceCode.verificationUri))"))
.foregroundStyle(.blue)
Text("and enter the following code:")
Text(deviceCode.userCode)
.padding()
Expand All @@ -63,13 +64,25 @@ struct ContentView: View {
var providerList: some View {
List(oauth.providers) { provider in
Button(provider.id) {
// Start the authorization flow (use .deviceCode for tvOS)
let grantType: OAuth.GrantType = .pkce(.init())
oauth.authorize(provider: provider, grantType: grantType)
authorize(provider: provider)
}
}
}


/// Starts the authorization process for the specified provider.
/// - Parameter provider: the provider to begin authorization for
private func authorize(provider: OAuth.Provider) {
#if canImport(WebKit)
// Use the PKCE grantType for iOS, macOS, visionOS
let grantType: OAuth.GrantType = .pkce(.init())
#else
// Use the Device Code grantType for tvOS, watchOS
let grantType: OAuth.GrantType = .deviceCode
#endif
// Start the authorization flow
oauth.authorize(provider: provider, grantType: grantType)
}

/// Reacts to oauth state changes by opening or closing authorization windows.
/// - Parameter state: the published state change
private func handle(state: OAuth.State) {
Expand All @@ -91,13 +104,13 @@ struct ContentView: View {
}

private func openWebView() {
#if !os(tvOS)
#if canImport(WebKit)
openWindow(id: .oauth)
#endif
}

private func dismissWebView() {
#if !os(tvOS)
#if canImport(WebKit)
dismissWindow(id: .oauth)
#endif
}
Expand Down
4 changes: 4 additions & 0 deletions OAuthSample/Classes/Extensions/OAuth+Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import OAuthKit
import SwiftUI

#if canImport(WebKit)

extension OAuth {

/// Provides a convenience enum that identifies application scene windows that can be opened or dismissed.
Expand Down Expand Up @@ -47,3 +49,5 @@ public extension DismissWindowAction {
callAsFunction(id: id.rawValue)
}
}

#endif
2 changes: 1 addition & 1 deletion OAuthSample/Classes/OAuthSampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct OAuthSampleApp: App {
ContentView()
}

#if !os(tvOS)
#if canImport(WebKit)
WindowGroup(id: .oauth) {
OAWebView(oauth: oauth)
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
![iOS 18.0+](https://img.shields.io/badge/iOS-18.0%2B-crimson.svg)
![macOS 15.0+](https://img.shields.io/badge/macOS-15.0%2B-skyblue.svg)
![tvOS 18.0+](https://img.shields.io/badge/tvOS-18.0%2B-blue.svg)
![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-magenta.svg)
![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-violet.svg)
![watchOS 11.0+](https://img.shields.io/badge/watchOS-11.0%2B-magenta.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-indigo.svg)](https://opensource.org/licenses/MIT)

# OAuthKit Sample
Expand Down