|
2 | 2 | // https://docs.swift.org/swift-book |
3 | 3 | import SwiftUI |
4 | 4 | import FirebaseAuth |
| 5 | +import FirebaseCore |
5 | 6 | import Combine |
6 | 7 |
|
| 8 | +public protocol FUIAuthProvider { |
| 9 | + var providerId: String { get } |
| 10 | +} |
| 11 | + |
| 12 | +public class FirebaseAuthSwiftUI { |
| 13 | + private var auth: Auth |
| 14 | + private var authProviders: [FUIAuthProvider] = [] |
| 15 | + |
| 16 | + public init(auth: Auth? = nil) { |
| 17 | + // Use the provided Auth instance or default |
| 18 | + self.auth = auth ?? Auth.auth() |
| 19 | + } |
7 | 20 |
|
8 | | -public protocol AuthListenerProtocol { |
9 | | - func onError(_ error: Error) |
10 | | - func onBeforeSignIn() |
11 | | - func onSignedIn(_ user: User) |
12 | | - func onCanceled() |
13 | | - func onCredentialReceived(_ credential: AuthCredential) |
14 | | - func onCredentialLinked(_ credential: AuthCredential) |
15 | | - // TODO - add when I get to this point |
16 | | - // func onMFARequired(_ resolver: MultiFactorResolver) |
| 21 | + public func authProviders(providers: [FUIAuthProvider]) { |
| 22 | + self.authProviders = providers |
| 23 | + } |
17 | 24 | } |
18 | 25 |
|
19 | | -public protocol AuthProviderProtocol { |
20 | | - associatedtype Listener: AuthListenerProtocol |
21 | | - associatedtype Credential: AuthCredential |
22 | | - |
23 | | - var auth: Auth { get } |
24 | | - var authListener: Listener { get set } |
25 | | - var providerId: String { get } |
| 26 | +// main auth view - can be composed of custom views or fallback to default views. We can also pass state upwards as opposed to callback. |
| 27 | +// Negates the need for a delegate used in UIKit |
| 28 | +public struct FUIAuthView: View { |
| 29 | + private var FUIAuth: FirebaseAuthSwiftUI |
| 30 | + private var authPickerView: any AuthPickerView |
26 | 31 |
|
27 | | - func signInWithCredential(_ credential: Credential) |
28 | | - func linkWithCredential(_ credential: Credential) |
29 | | -} |
30 | 32 |
|
31 | | -public enum AuthAction { |
32 | | - /// Performs user sign in |
33 | | - case signIn |
| 33 | + public init(FUIAuth: FirebaseAuthSwiftUI,_authPickerView: some AuthPickerView = FUIAuthPicker()) { |
| 34 | + self.FUIAuth = FUIAuth |
| 35 | + self.authPickerView = _authPickerView |
34 | 36 |
|
35 | | - /// Creates a new account with for a provided credential |
36 | | - case signUp |
| 37 | + } |
| 38 | + |
| 39 | + public var body: some View { |
| 40 | + VStack { |
| 41 | + AnyView(authPickerView) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
37 | 45 |
|
38 | | - /// Links a provided credential with currently signed in user account |
39 | | - case link |
40 | 46 |
|
41 | | - /// Disables automatic credential handling. |
42 | | - /// It's up to the user to decide what to do with the obtained credential. |
43 | | - case none |
| 47 | +public protocol AuthPickerView: View { |
| 48 | + var title: String { get } |
44 | 49 | } |
45 | 50 |
|
46 | | -open class AuthProvider<Listener: AuthListenerProtocol>: AuthProviderProtocol { |
47 | | - public var auth: Auth = Auth.auth() |
48 | | - public var authListener: Listener |
49 | | - public var providerId: String |
50 | | - |
51 | | - public init(listener: Listener, providerId: String) { |
52 | | - self.authListener = listener |
53 | | - self.providerId = providerId |
54 | | - } |
55 | | - |
56 | | - var shouldUpgradeAnonymous: Bool { |
57 | | - return Auth.auth().currentUser?.isAnonymous ?? false |
| 51 | +public struct FUIAuthPicker: AuthPickerView { |
| 52 | + public init(title: String? = nil) { |
| 53 | + self.title = title ?? "Auth Picker View" |
58 | 54 | } |
59 | | - |
60 | | - public func signInWithCredential(_ credential: AuthCredential) { |
61 | | - authListener.onBeforeSignIn() |
62 | | - auth.signIn(with: credential) { [weak self] result, error in |
63 | | - if let error = error { |
64 | | - self?.authListener.onError(error) |
65 | | - } else if let user = result?.user { |
66 | | - self?.authListener.onSignedIn(user) |
67 | | - } |
| 55 | + public var title: String = "Main View" |
| 56 | + public var body: some View { |
| 57 | + VStack { |
| 58 | + Text(title) |
| 59 | + .font(.largeTitle) |
| 60 | + .padding() |
68 | 61 | } |
69 | 62 | } |
70 | | - |
71 | | - public func linkWithCredential(_ credential: AuthCredential) { |
72 | | - authListener.onCredentialReceived(credential) |
73 | | - guard let user = auth.currentUser else { return } |
74 | | - |
75 | | - user.link(with: credential) { [weak self] result, error in |
76 | | - if let error = error { |
77 | | - self?.authListener.onError(error) |
78 | | - } else { |
79 | | - self?.authListener.onCredentialLinked(credential) |
80 | | - } |
81 | | - } |
82 | | - } |
83 | | - |
84 | | - func onCredentialReceived(credential: AuthCredential, action: AuthAction) { |
85 | | - switch action { |
86 | | - case .link: |
87 | | - linkWithCredential(credential) |
88 | | - case .signIn, .signUp: |
89 | | - // Only email provider has a different action for sign in and sign up |
90 | | - // and implements its own sign up logic. |
91 | | - if shouldUpgradeAnonymous { |
92 | | - linkWithCredential(credential) |
93 | | - } else { |
94 | | - signInWithCredential(credential) |
95 | | - } |
96 | | - case .none: |
97 | | - authListener.onCredentialReceived(credential) |
98 | | - } |
99 | | - } |
100 | | - // TODO - fetchSignInMethodsForEmail/fetchProvidersForEmail is deprecated |
101 | 63 | } |
102 | | - |
0 commit comments