hack_auth 0.1.0
hack_auth: ^0.1.0 copied to clipboard
Plug-and-play Firebase Email/Password authentication for Flutter. Get Login, Register, Forgot Password screens and an AuthGate widget working in under 5 minutes — fully themeable, zero boilerplate.
import 'package:flutter/material.dart';
import 'package:hack_auth/hack_auth.dart';
import 'firebase_options.dart';
/// ──────────────────────────────────────────────────────────────────────────
/// Example app for hack_auth package
///
/// To run this example you need:
/// 1. A Firebase project with Email/Password auth enabled.
/// 2. Replace the FirebaseOptions below with your own credentials.
/// (Run `flutterfire configure` in this directory for the easiest setup.)
/// ──────────────────────────────────────────────────────────────────────────
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// ── Step 1: Initialize HackAuth with your Firebase config ────────────────
await HackAuth.initialize(
firebaseOptions: DefaultFirebaseOptions.currentPlatform,
// ── Step 2 (optional): Customise the look & feel ─────────────────────
theme: const HackAuthTheme(
primaryColor: Color(0xFF6C63FF),
backgroundColor: Color(0xFF0F0F1A),
surfaceColor: Color(0xFF1C1C2E),
borderRadius: 14,
),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'HackAuth Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.dark(
primary: const Color(0xFF6C63FF),
surface: const Color(0xFF0F0F1A),
),
useMaterial3: true,
),
// ── Step 3: Use AuthGate as your root ─────────────────────────────
home: AuthGate(
authenticated: const HomePage(),
unauthenticated: LoginPage(
onLoginSuccess: () {
// AuthGate will automatically switch to HomePage.
// No manual navigation needed!
},
),
),
);
}
}
// ── Home page shown when authenticated ─────────────────────────────────────
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final user = HackAuth.currentUser;
const bgColor = Color(0xFF0F0F1A);
const accentColor = Color(0xFF6C63FF);
return Scaffold(
backgroundColor: bgColor,
appBar: AppBar(
backgroundColor: const Color(0xFF1C1C2E),
elevation: 0,
title: const Text(
'HackAuth Demo',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
actions: [
IconButton(
icon: const Icon(Icons.logout, color: Colors.white70),
tooltip: 'Logout',
onPressed: () async {
await HackAuth.logout();
// AuthGate reacts automatically — no navigation code needed
},
),
],
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Avatar circle
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: const LinearGradient(
colors: [Color(0xFF6C63FF), Color(0xFFB06AB3)],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
boxShadow: [
BoxShadow(
color: accentColor.withValues(alpha: 0.4),
blurRadius: 24,
offset: const Offset(0, 8),
),
],
),
child: const Icon(Icons.person, color: Colors.white, size: 40),
),
const SizedBox(height: 24),
const Text(
'🎉 You\'re logged in!',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
Text(
user?.displayName?.isNotEmpty == true
? 'Hello, ${user!.displayName}!'
: 'Hello, ${user?.email ?? "User"}!',
style: const TextStyle(color: Colors.white70, fontSize: 16),
),
const SizedBox(height: 8),
Text(
'UID: ${user?.uid ?? "—"}',
style: const TextStyle(color: Colors.white38, fontSize: 12),
),
const SizedBox(height: 40),
ElevatedButton.icon(
onPressed: () async => await HackAuth.logout(),
icon: const Icon(Icons.logout),
label: const Text('Logout'),
style: ElevatedButton.styleFrom(
backgroundColor: accentColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
horizontal: 32, vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
],
),
),
),
);
}
}