Skip to content

sugijotaro/FirebaseAuthEasier

Repository files navigation

FirebaseAuthEasier

日本語READMEはこちら

Swift Package Manager Compatible Platform License Swift

FirebaseAuthEasier is a Swift package that makes it easy to integrate Firebase Authentication (Apple/Google sign-in) into SwiftUI apps.

image

Features

  • Provides Apple, Google, and Anonymous sign-in UI/logic in one package
  • Simple API with customizable UI components
  • Built on top of official Firebase/GoogleSignIn SDKs
  • Supports iOS 15 and later
  • Flexible authentication operation APIs via FirebaseAuthService
  • SignInButton components can be used individually with flexible UI customization

Supported Platforms

  • iOS 15.0+

Dependencies

Installation

Add the package using Swift Package Manager.

.package(url: "https://github.com/sugijotaro/FirebaseAuthEasier.git", from: "1.0.0")

In Xcode:

  1. Go to "File" → "Add Package Dependencies..." and enter the URL above
  2. Select "FirebaseAuthEasier" to add

Initial Setup

  1. Follow the Firebase official documentation to add GoogleService-Info.plist
  2. Initialize Firebase in your AppDelegate or @main App
  3. Enable "Sign in with Apple", "Sign in with Google", and "Anonymous" in Firebase Console
  4. Add "Sign in with Apple" capability in Xcode project settings
  5. For Google authentication, obtain OAuth client ID from Google Cloud Console and configure it in your Firebase project

For more detailed setup instructions, see this article (Japanese only)

Basic Usage

import SwiftUI
import FirebaseAuthEasier

struct ContentView: View {
    var body: some View {
        FirebaseAuthView()
    }
}

Demo App

A demo app is available in the sample/FirebaseAuthEasierDemoApp directory.

You can use this app to see FirebaseAuthEasier in action and as a reference for integration.

FirebaseAuthView Customization

FirebaseAuthView can be flexibly customized with the following initializer parameters:

public init(
    providers: [SignInProviderType]? = nil,
    labelType: SignInButtonLabelType = .signIn,
    termsOfServiceURL: URL? = nil,
    privacyPolicyURL: URL? = nil,
    onSignInStart: ((SignInProviderType) -> Void)? = nil,
    didSignIn: ((Result<AuthDataResult, Error>) -> Void)? = nil,
    @ViewBuilder content: @escaping () -> Content = { FirebaseAuthDefaultContentView() }
)

Parameter Details and Customization Examples

providers (Default: [.apple, .google])

Specify which sign-in providers to display.

FirebaseAuthView(providers: [.apple]) // Apple sign-in only
FirebaseAuthView(providers: [.apple, .google, .anonymous]) // All providers including anonymous

labelType (Default: .signIn)

Specify the button label type (.signIn, .signUp, .continue).

FirebaseAuthView(labelType: .signUp)

termsOfServiceURL (Default: nil)

URL for Terms of Service. If provided, a legal message will be displayed at the bottom of the sign-in screen.

FirebaseAuthView(termsOfServiceURL: URL(string: "https://example.com/terms"))

privacyPolicyURL (Default: nil)

URL for Privacy Policy. If provided, a legal message will be displayed at the bottom of the sign-in screen.

FirebaseAuthView(privacyPolicyURL: URL(string: "https://example.com/privacy"))

content (Default: FirebaseAuthDefaultContentView)

Customize the view displayed at the top of the sign-in screen.

FirebaseAuthView {
    VStack {
        Text("Welcome!")
        Image(systemName: "person.circle")
    }
}

onSignInStart (Default: nil)

Closure called when sign-in process starts. Receives the provider type.

FirebaseAuthView(onSignInStart: { provider in
    print("Sign-in started: \(provider)")
})

didSignIn (Default: nil)

Closure called when sign-in completes. Receives success/failure result.

FirebaseAuthView(didSignIn: { result in
    switch result {
    case .success(let authData):
        // Handle successful sign-in
    case .failure(let error):
        // Handle error
    }
})

Combined Example

struct ContentView: View {
    var body: some View {
        FirebaseAuthView(
            providers: [.apple, .google, .anonymous],
            labelType: .continue,
            termsOfServiceURL: URL(string: "https://example.com/terms")!,
            privacyPolicyURL: URL(string: "https://example.com/privacy")!,
            onSignInStart: { provider in
                print("Sign-in started with provider: \(provider)")
            },
            didSignIn: { result in
                switch result {
                case .success(let authResult):
                    if authResult.user.isAnonymous {
                        print("Anonymous sign-in successful")
                    } else {
                        print("Sign-in successful")
                    }
                case .failure(let error):
                    print("Sign-in failed: \(error.localizedDescription)")
                }
            }
        ) {
            VStack {
                Image(systemName: "person.circle")
                    .imageScale(.large)
                    .foregroundStyle(.tint)
                Text("Welcome!")
            }
        }
    }
}

Flexible Authentication Operations with FirebaseAuthService

FirebaseAuthService provides convenient APIs for detailed authentication operations such as sign-in, sign-out, anonymous authentication, re-authentication, and account deletion.

import FirebaseAuthEasier

let authService = FirebaseAuthService()

// Anonymous sign-in
authService.signInAnonymously { result in /* ... */ }

// Sign-out
authService.signOut { result in /* ... */ }

// Link authentication providers
// Account deletion
// ...and more

Individual Use and Customization of SignInButton Component

SignInButton provides Apple/Google sign-in button UI that can be used individually, with fine-grained customization for colors, labels, corner radius, borders, and more.

import SwiftUI
import FirebaseAuthEasier

SignInButton(
    provider: .apple,
    buttonStyle: .black,
    labelStyle: .titleAndIcon,
    labelType: .signIn,
    cornerStyle: .radius(12),
    hasBorder: true
) {
    // Button tap handling
}

Contributing

We welcome contributions to this project!

  • Report bugs or request features via Issues
  • Pull requests are very welcome
  • Feel free to reach out with improvement suggestions or feedback

Important Notes

  • Authentication will fail if Firebase/GoogleSignIn setup is not configured correctly
  • Google authentication requires configuration in Google Cloud Console
  • Apple sign-in only works on physical devices
  • When using Apple sign-in, make sure to enable it in Firebase Console and add the capability in Xcode

License

MIT © Jotaro Sugiyama

About

A Swift package for simple Firebase Authentication integration in SwiftUI apps.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages