diff --git a/README.md b/README.md index 00993f7..0d8485a 100644 --- a/README.md +++ b/README.md @@ -111,10 +111,28 @@ RxSignInView( ) ``` +**With callbacks:** + +```swift +RxSignInView( + manager: authManager, + onAuthSuccess: { + // Navigate to home, fetch user data, etc. + }, + onAuthFailed: { error in + // Log error, show custom alert, etc. + } +) +``` + **Advanced (fully custom header with ViewBuilder):** ```swift -RxSignInView(manager: authManager) { +RxSignInView( + manager: authManager, + onAuthSuccess: { print("Signed in!") }, + onAuthFailed: { error in print("Failed: \(error)") } +) { VStack { Image("Logo") .resizable() diff --git a/Sources/RxAuthSwiftUI/RxSignInView.swift b/Sources/RxAuthSwiftUI/RxSignInView.swift index eead86b..e124c13 100644 --- a/Sources/RxAuthSwiftUI/RxSignInView.swift +++ b/Sources/RxAuthSwiftUI/RxSignInView.swift @@ -5,16 +5,22 @@ public struct RxSignInView: View { @Bindable private var manager: OAuthManager private let appearance: RxSignInAppearance private let customHeader: Header? + private let onAuthSuccess: (() -> Void)? + private let onAuthFailed: ((Error) -> Void)? // MARK: - Simple Init (appearance struct) public init( manager: OAuthManager, - appearance: RxSignInAppearance = RxSignInAppearance() + appearance: RxSignInAppearance = RxSignInAppearance(), + onAuthSuccess: (() -> Void)? = nil, + onAuthFailed: ((Error) -> Void)? = nil ) where Header == Never { self.manager = manager self.appearance = appearance self.customHeader = nil + self.onAuthSuccess = onAuthSuccess + self.onAuthFailed = onAuthFailed } // MARK: - Advanced Init (ViewBuilder for custom header) @@ -22,11 +28,15 @@ public struct RxSignInView: View { public init( manager: OAuthManager, appearance: RxSignInAppearance = RxSignInAppearance(), + onAuthSuccess: (() -> Void)? = nil, + onAuthFailed: ((Error) -> Void)? = nil, @ViewBuilder header: () -> Header ) { self.manager = manager self.appearance = appearance self.customHeader = header() + self.onAuthSuccess = onAuthSuccess + self.onAuthFailed = onAuthFailed } public var body: some View { @@ -65,8 +75,9 @@ public struct RxSignInView: View { Task { do { try await manager.authenticate() + onAuthSuccess?() } catch { - // Error is handled by the manager + onAuthFailed?(error) } } }