Repository navigation
Authentication & Authorization
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Security Considerations
- Conclusion
This document explains the workspace-based local authentication and authorization system for PersonalLearningPro. The platform has migrated from a Firebase-centric model to a self-hosted, PostgreSQL-backed identity system that supports multi-tenancy via "Workspaces".
Key features:
- Local email/password registration and login
- Multi-tenant Workspace architecture (Owner, Admin, Member roles)
- Secure session handling using JWT Access Tokens and PostgreSQL-backed Refresh Tokens
- Tokenized invitation system for onboarding members and students
The authentication system is unified across the stack:
-
Client:
AuthProvider(React Context) manages session lifecycle and user state. - Server: Express middleware and specialized routes handle token issuance, rotation, and RBAC.
- Shared: Zod schemas ensure consistent validation of credentials and workspace data.
graph TB
subgraph "Client (Vite/React)"
Ctx["Auth Context<br/>firebase-auth-context.tsx"]
UI["Auth Dialog<br/>firebase-auth-dialog.tsx"]
Invite["Invite Acceptance<br/>accept-invite.tsx"]
end
subgraph "Server (Node.js/Express)"
AuthLib["Auth Utils<br/>server/lib/auth-workspace.ts"]
Routes["Auth Routes<br/>server/routes/auth.ts"]
MW["Auth Middleware<br/>server/middleware.ts"]
PG["DB Queries<br/>server/lib/pg-queries.ts"]
end
Ctx --> UI
Ctx --> Invite
Routes --> AuthLib
Routes --> PG
MW --> AuthLib
-
auth-workspace.ts: Core logic for token hashing, permission mapping, and payload generation. -
firebase-auth-context.tsx: (Renamed internally toAuthProvider) Centralized state for the frontend. -
auth.ts(Routes): Endpoints for signup, login, refresh, logout, and password management. -
middleware.ts: Guards for enforcing session presence (authenticateToken) and workspace roles. -
pg-queries.ts: Relational database operations for users, workspaces, and memberships.
The system uses a Dual-Token Cookie-based approach:
-
Access Token (JWT): Short-lived (15m), stored in an
HttpOnlycookie. ContainsuserIdandsessionId. -
Refresh Token (Opaque): Long-lived (30d), stored in an
HttpOnlycookie and thesessionstable in PostgreSQL. -
RBAC: Permissions are derived from the user's active
workspace_membership.
sequenceDiagram
participant Browser as "Browser"
participant Server as "Express Server"
participant PG as "PostgreSQL"
Browser->>Server : "POST /api/auth/login"
Server->>PG : "Verify credentials"
PG-->>Server : "User + Workspace Context"
Server->>Server : "Sign JWT Access Token"
Server->>PG : "Store Refresh Token in 'sessions'"
Server-->>Browser : "Set HttpOnly Cookies (Access + Refresh)"
Browser->>Server : "Subsequent API Call (Cookies included)"
Server->>Server : "Verify JWT"
Server-->>Browser : "Authorized Data"
Registration now follows a "Workspace-first" model. When a user signs up:
- A User record is created in PostgreSQL.
- A Workspace is automatically created with the user as the Owner.
- A WorkspaceMembership links the two.
To mitigate session hijacking, the system implements Refresh Token Rotation:
- Every time
/api/auth/refreshis called, the old refresh token is invalidated and a new one is issued. - If a leaked refresh token is reused, the entire session chain is invalidated.
Roles are scoped to the Active Workspace:
-
owner: Full control, billing, member management. -
admin: Content management, invites, analytics. -
member: Standard access to workspace features.
- PostgreSQL: Primary transactional store for identity.
- JWT: Stateless session verification.
- Cookie-Parser: Secure cookie extraction.
- BcryptJS: Password hashing.
- HttpOnly Cookies: Prevents XSS-based token theft.
-
CSRF Protection: Mitigated via
SameSite=Laxand custom headers. - Hashed Tokens: Invitation and reset tokens are never stored in plain text.
The new authentication system provides a robust, scalable, and secure foundation for multi-tenant educational workflows, removing the external dependency on Firebase while enhancing security and data sovereignty.