basific 1.0.0
basific: ^1.0.0 copied to clipboard
A comprehensive Flutter package for authentication and user management with Supabase integration. Provides ready-to-use login, registration, and user management components.
Basific #
A comprehensive Flutter package for authentication and user management with Supabase integration. Provides ready-to-use login, registration, and user management components.
Features #
- 🔐 Complete Authentication System - Login and registration with validation
- 🆔 Multiple Login Methods - Support for both email and username login
- 👥 User Management - CRUD operations for user accounts
- 👑 Role Management - Built-in admin and user roles support
- 🎨 Customizable UI - Themeable components that match your app design
- 🗃️ Supabase Integration - Built-in support for Supabase backend
- 📱 Ready-to-use Components - Drop-in widgets for common auth flows
- ⚙️ Configurable - Flexible table and column name mapping
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
basific: ^1.0.0
Quick Start #
1. Initialize Basific #
import 'package:basific/basific.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Basific with your Supabase credentials
await Basific.initialize(
BasificConfig(
supabaseUrl: 'YOUR_SUPABASE_URL',
supabaseAnonKey: 'YOUR_SUPABASE_ANON_KEY',
),
);
runApp(MyApp());
}
2. Database Setup #
You can use the built-in setup page in the example app, or run this SQL in your Supabase database:
-- 創建 profiles 表格以支援使用者名稱登入
CREATE TABLE public.profiles (
id UUID REFERENCES auth.users(id) ON DELETE CASCADE PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
display_name TEXT UNIQUE,
full_name TEXT,
avatar_url TEXT,
role TEXT DEFAULT 'user' CHECK (role IN ('admin', 'user')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- 設定 RLS (Row Level Security)
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
-- 允許所有人讀取 profiles
CREATE POLICY "Profiles are viewable by everyone"
ON public.profiles FOR SELECT
USING (true);
-- 只允許用戶更新自己的 profile
CREATE POLICY "Users can update own profile"
ON public.profiles FOR UPDATE
USING (auth.uid() = id);
-- 只允許用戶插入自己的 profile
CREATE POLICY "Users can insert own profile"
ON public.profiles FOR INSERT
WITH CHECK (auth.uid() = id);
3. Use Components #
// Login Page - supports both email and username
BasificLoginPage(
onLoginSuccess: (user) {
// Navigate to home
},
)
// Register a new user (optionally with role)
final result = await Basific.register(
email: 'user@example.com',
password: 'password123',
displayName: 'User Name',
role: 'admin', // Optional, defaults to 'user'
);
// Login programmatically with email
final result = await Basific.login('user@example.com', 'password');
// Check user role
if (user.isAdmin) {
// Show admin features
}
// Use role-aware components
HomePage(
currentUser: user, // Will automatically show/hide admin features
)
Login Methods #
Basific supports two login methods:
- Email Login: Traditional email and password authentication
- Username Login: Users can login with their username instead of email
The login page automatically detects whether the input is an email (contains @) or a username, and handles the authentication accordingly.
When using username login, Basific:
- Checks if the input contains @ (email format)
- If not, queries the
accounttable to find the corresponding user ID - Retrieves the email from Supabase auth.users table
- Performs authentication using the email
Database Schema #
For username login to work, you need both:
- Supabase Auth Users (managed automatically)
- Custom Account Table:
CREATE TABLE account (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
account TEXT UNIQUE NOT NULL, -- This is the username
email TEXT UNIQUE NOT NULL, -- Email for login (synced with auth.users)
password TEXT NOT NULL, -- Not used in auth, kept for compatibility
name TEXT NOT NULL,
level TEXT DEFAULT 'user'
);
Requirements #
- Flutter 3.0.0+
- Dart 3.8.1+
- Supabase project