Package Exports
- supabase-user-manager-with-typescript
Readme
User Manager Library
Complete TypeScript library for Supabase user management with authentication, sessions, and status system
โจ Features
- ๐ Complete Authentication System - Sign up, sign in, password reset, email verification
- ๐ Advanced Session Management - Auto-refresh, multi-tab sync, persistent sessions
- ๐ User Status Progression - Points-based level system (Basic โ Silver โ Gold โ Platinum)
- ๐ Real Database Integration - Supabase database functions and RLS policies
- ๐ฏ Event-Driven Architecture - Real-time updates and state synchronization
- ๐ก๏ธ Comprehensive Error Handling - User-friendly error messages and recovery
- ๐ Full TypeScript Support - Complete type definitions and IntelliSense
- ๐งช Production Ready - Thoroughly tested with comprehensive test suite
๐ Quick Start
Installation
npm install @user/user-manager @supabase/supabase-jsBasic Usage
import {UserManager} from "@user/user-manager";
// Initialize with your Supabase config
const userManager = UserManager.getInstance({
supabase: {
url: "your-supabase-url",
anonKey: "your-supabase-anon-key",
},
events: {
enableLogging: true,
},
});
// Initialize the manager
await userManager.initialize();
// Sign up a new user
const result = await userManager.signUp({
email: "user@example.com",
password: "securePassword123",
});
if (result.success) {
console.log("User signed up successfully!");
}๐ Core Components
Authentication System
// Sign up with email verification
await userManager.signUp({
email: "user@example.com",
password: "password123",
});
// Sign in
await userManager.signIn({
email: "user@example.com",
password: "password123",
});
// Password reset
await userManager.resetPassword({
email: "user@example.com",
});
// Sign out
await userManager.signOut();Session Management
// Get current session
const session = userManager.getCurrentSession();
// Refresh session manually
await userManager.refreshSession();
// Session automatically refreshes and syncs across tabs
userManager.on("session:refreshed", (data) => {
console.log("Session refreshed:", data.session);
});User Status System
// Get user status
const status = await userManager.getUserStatus();
console.log(`Level: ${status.data.status}, Points: ${status.data.points}`);
// Add points
await userManager.addUserPoints(500);
// Update total points
await userManager.updateUserPoints(2500);
// Get progress to next level
const progress = userManager.getStatusProgress();
console.log(`${progress.pointsToNext} points to ${progress.nextLevel}`);Event System
// Listen to authentication events
userManager.on("auth:signedIn", (data) => {
console.log("User signed in:", data.user.email);
});
userManager.on("auth:signedOut", (data) => {
console.log("User signed out");
});
// Listen to status changes
userManager.on("status:levelChanged", (data) => {
console.log(`Level up! ${data.previousStatus} โ ${data.newStatus}`);
});
// Listen to session events
userManager.on("session:started", (data) => {
console.log("Session started");
});๐๏ธ Database Setup
The library requires specific database functions and tables. Run this SQL in your Supabase SQL Editor:
-- Copy and paste the contents of supabase-setup.sql
-- This creates the user_status table, RLS policies, and database functionsThe setup includes:
user_statustable with RLS policies- Status calculation functions
- Points management functions
- Automatic status level progression
๐ฏ Status Level System
| Level | Points Required | Benefits |
|---|---|---|
| Basic | 0 - 999 | Basic access, Community support |
| Silver | 1,000 - 4,999 | Priority support, Advanced features |
| Gold | 5,000 - 9,999 | Premium support, Beta access, Exclusive content |
| Platinum | 10,000+ | VIP support, Early access, Custom integrations |
๐ง Configuration
interface UserManagerConfig {
supabase: {
url: string;
anonKey: string;
};
events?: {
enableLogging?: boolean;
maxListeners?: number;
};
}๐ API Reference
Core Methods
UserManager.getInstance(config)- Get singleton instanceinitialize()- Initialize the managersignUp(credentials)- Register new usersignIn(credentials)- Authenticate usersignOut()- Sign out current userresetPassword(request)- Send password reset email
Session Methods
getCurrentSession()- Get current sessionrefreshSession()- Manually refresh sessiongetSessionState()- Get session information
Status Methods
getUserStatus()- Get current user statusupdateUserPoints(points)- Set total pointsaddUserPoints(points)- Add points to current totalgetStatusProgress()- Get progress to next levelgetStatusLevelInfo(level)- Get level information
Utility Methods
getCurrentUser()- Get current userisAuthenticated()- Check authentication statusgetUserState()- Get complete user state
๐ช Events
Authentication Events
auth:signedIn- User signed inauth:signedOut- User signed outauth:signUpComplete- Sign up completedauth:passwordResetSent- Password reset email sent
Session Events
session:started- Session startedsession:ended- Session endedsession:refreshed- Session refreshedsession:restored- Session restored from storage
Status Events
status:updated- Status data updatedstatus:levelChanged- User level changedstatus:pointsAdded- Points added to user
Error Events
error:general- General error occurrederror:network- Network errorerror:validation- Validation error
๐งช Testing
The library includes a comprehensive test app with interactive test suites:
npm run devNavigate to the test suites to validate:
- Event Emitter functionality
- User Manager core features
- Email authentication flows
- Session management
- User status system
- Error handling
๐๏ธ Development
# Install dependencies
npm install
# Start development server
npm run dev
# Type check
npm run type-check
# Build library
npm run build
# Clean build artifacts
npm run clean๐ฆ Bundle Size
- Raw: ~55KB
- Gzipped: ~11KB
- Dependencies: Only @supabase/supabase-js (peer dependency)
๐ค Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Built with Supabase for backend services
- Powered by TypeScript and Vite
- Inspired by modern authentication and user management needs
Made with โค๏ธ for the developer community