π Learn how to build and use this package: https://www.swiftful-thinking.com/offers/REyNLwwH
A reusable AuthManager for Swift applications, built for Swift 6. Includes @Observable
support.
Pre-built dependencies*:
- Mock: Included
- Firebase: https://github.com/SwiftfulThinking/SwiftfulAuthenticatingFirebase
* Created another? Send the url in issues! π₯³
- β Sign In Apple
- β Sign In Google
- β Sign In Anonymous
Task {
do {
let (userAuthInfo, isNewUser) = try await authManager.signInApple()
// User is signed in
if isNewUser {
// New user -> Create user profile in Firestore
} else {
// Existing user -> sign in
}
} catch {
// User auth failed
}
}
Details (Click to expand)
let authManager = AuthManager(services: any AuthService, logger: LogManager?)
#if DEBUG
let authManager = AuthManager(service: MockAuthService(), logger: logManager)
#else
let authManager = AuthManager(service: FirebaseAuthService(), logger: logManager)
#endif
Text("Hello, world!")
.environment(authManager)
Details (Click to expand)
AuthManager
is initialized with a AuthService
. This is a public protocol you can use to create your own dependency.
MockPurchaseService
is included for SwiftUI previews and testing.
// User is not yet authenticated
let service = MockAuthService(user: nil)
// User is already authenticated
let service = MockAuthService(user: .mock)
Other services are not directly included, so that the developer can pick-and-choose which dependencies to add to the project.
You can create your own AuthService
by conforming to the protocol:
public protocol AuthService: Sendable {
func getAuthenticatedUser() -> UserAuthInfo?
func addAuthenticatedUserListener() -> AsyncStream<UserAuthInfo?>
func signIn(option: SignInOption) async throws -> (user: UserAuthInfo, isNewUser: Bool)
func signOut() throws
func deleteAccount() async throws
}
Details (Click to expand)
The manager will automatically fetch and listen for an authenticated user on launch, via getAuthenticatedUser
and addAuthenticatedUserListener
.
let userId = authManager.auth.uid
let authUser = authManager.auth
// Throwing method for convenience with async/await
let uid = try authManager.getAuthId()
try authManager.signOut()
try await authManager.deleteAccount()
Details (Click to expand)
- Xcode Project Navigator -> Target -> Signing & Capabilities -> + Capability -> Sign in with Apple (requires Apple Developer Account)
import SwiftfulAuthUI
SignInAppleButtonView()
.frame(height: 50)
try await authManager.signInApple()
Details (Click to expand)
- Firebase Console -> Project Settings -> Your apps -> GoogleService-Info.plist
- GoogleService-Info.plist -> REVERSED_CLIENT_ID
- Xcode Project Navigator -> Target -> Info -> URL Types -> add REVERSED_CLIENT_ID as URL Schemes value
import SwiftfulAuthUI
SignInGoogleButtonView()
.frame(height: 50)
try await authManager.signInGoogle(GIDClientID: clientId)