Authentication Service Documentation
📚 Overview
The Authentication Service (src/services/authService.ts) handles user registration, login (via standard JWT flow), and team onboarding. Unlike managed solutions (Clerk, Auth0), this project uses a custom implementation to provide full control over the database schema and multi-tenancy logic.
🔐 Core Features
1. Registration (Admin/Owner)
- Function:
register(data) - Flow:
- Validates email uniqueness.
- Hashes password using
bcryptjs. - Transaction:
- Creates
User. - Creates
Team(slug generated from company name). - Creates
TeamMemberwith roleADMIN. - Creates
AuditLog(Action:ORG_CREATED).
- Creates
2. Invitation-based Registration (Member)
- Function:
registerWithInvite(data) - Flow:
- Validates implementation token string.
- Checks expiration and email mismatch.
- Transaction:
- Creates
User. - Adds
TeamMemberlinked to the inviting team. - Deletes the used
Invitation. - Creates
AuditLog(Action:MEMBER_JOINED).
- Creates
🛠️ Data Models (Schema)
| Table | Description |
|---|---|
users | Stores global user profile (Name, Email, Password Hash). |
teams | Represents the "Tenant" or "Organization". |
team_members | Link between User and Team. Contains role (ADMIN, MEMBER, OBSERVER). |
invitations | Temporary tokens for inviting new members. |
🛡️ Security Implementation
- Password Hashing: BCrypt (Salt Rouds: 10).
- Session: JWT based (implemented in
src/lib/auth.ts/ Middleware). - RBAC: Roles are checked at the Service level and Middleware level.
🚀 Usage Example
import { AuthService } from '@/services/authService';
// Register a new company owner
const result = await AuthService.register({
name: "John CEO",
email: "john@startup.com",
password: "securePassword123",
companyName: "John Startup Inc."
});
// Result contains { user, team }
console.log(result.team.slug); // "john-startup-inc-1735..."
⚠️ Known Limitations & Risks
- No MFA: Currently does not support Multi-Factor Authentication.
- Session Management: JWT revocation relies on short expiry times; no blacklist mechanism yet.
- Social Login: OAuth flow is handled separately in
oauthService.ts.