codifyiq_firebase_authentication 1.0.0 copy "codifyiq_firebase_authentication: ^1.0.0" to clipboard
codifyiq_firebase_authentication: ^1.0.0 copied to clipboard

Firebase authentication for Flutter apps. Provides Google and Apple sign-in with multi-environment Firebase support.

example/example.dart

// ignore_for_file: depend_on_referenced_packages

import 'package:codifyiq_firebase_authentication/codifyiq_firebase_authentication.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

/// Minimal Flutter app demonstrating Google and Apple sign-in.
///
/// Replace the [FirebaseOptions] below with your own values generated by
/// `flutterfire configure` (typically `DefaultFirebaseOptions.currentPlatform`).
Future<void> main() async {
  // Required before any Firebase or platform plugin is used.
  WidgetsFlutterBinding.ensureInitialized();

  await FirebaseAuthInitializer.initialize(
    firebaseOptions: const FirebaseOptions(
      apiKey: 'your-api-key',
      appId: 'your-app-id',
      messagingSenderId: 'your-sender-id',
      projectId: 'your-project-id',
    ),
  );

  runApp(const ExampleApp());
}

/// A single shared auth service for the app.
final authService = FirebaseAuthService();

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Firebase Auth Example',
      home: StreamBuilder<User?>(
        // Rebuilds whenever the user signs in or out — this drives navigation.
        stream: FirebaseAuthInitializer.authStateChanges(),
        builder: (context, snapshot) {
          final user = snapshot.data;
          return user == null ? const SignInScreen() : HomeScreen(user: user);
        },
      ),
    );
  }
}

class SignInScreen extends StatelessWidget {
  const SignInScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          spacing: 12,
          children: [
            ElevatedButton.icon(
              icon: const Icon(Icons.login),
              label: const Text('Continue with Google'),
              onPressed: () =>
                  _handleSignIn(context, authService.signInWithGoogle()),
            ),
            ElevatedButton.icon(
              icon: const Icon(Icons.apple),
              label: const Text('Continue with Apple'),
              onPressed: () =>
                  _handleSignIn(context, authService.signInWithApple()),
            ),
          ],
        ),
      ),
    );
  }

  /// Awaits a sign-in call and surfaces failures via a SnackBar. On success
  /// the [StreamBuilder] rebuilds automatically, so no navigation is needed.
  Future<void> _handleSignIn(
    BuildContext context,
    Future<FirebaseAuthResult> pending,
  ) async {
    final result = await pending;
    if (!context.mounted) return;
    switch (result) {
      case FirebaseAuthSuccess():
        break; // Auth-state stream handles the transition.
      case FirebaseAuthFailure(:final message):
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text(message)));
      case FirebaseAuthCancelled():
        break; // User dismissed the sign-in sheet.
    }
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key, required this.user});

  final User user;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Signed in')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          spacing: 12,
          children: [
            Text('Signed in as ${user.email ?? 'unknown'}'),
            ElevatedButton(
              onPressed: authService.signOut,
              child: const Text('Sign out'),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
150
points
32
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Firebase authentication for Flutter apps. Provides Google and Apple sign-in with multi-environment Firebase support.

Repository (GitHub)
View/report issues

Topics

#authentication #firebase #firebase-auth #google-sign-in #apple-sign-in

License

MIT (license)

Dependencies

crypto, firebase_auth, firebase_core, flutter, google_sign_in, sign_in_with_apple

More

Packages that depend on codifyiq_firebase_authentication