Add authentication

This commit is contained in:
2025-07-18 19:56:07 -04:00
parent 3b9c0cc31b
commit 1121e5c6ff
25 changed files with 3047 additions and 109 deletions

View File

@@ -16,8 +16,8 @@ HRIStudio is a web-based platform for standardizing and improving Wizard of Oz (
- **Relations**: All foreign keys and table relationships configured
- **Indexes**: Performance optimization indexes in place
#### 2. API Infrastructure (95%)
All major tRPC routers implemented:
#### 2. API Infrastructure (100%) ✅
All major tRPC routers implemented and schema-aligned:
**Authentication & Users**
- `auth` router: Login, logout, registration, session management
@@ -41,7 +41,29 @@ All major tRPC routers implemented:
- `collaboration` router: Comments, attachments, resource sharing
- `admin` router: System stats, settings, audit logs, backup management
#### 3. Project Structure (100%)
#### 3. Authentication System (100%)
- **NextAuth.js v5** configured with email/password authentication
- **JWT session strategy** implemented with role support
- **Protected routes** with middleware authentication
- **tRPC authentication** procedures (protected, admin)
- **Complete auth flow**: signin, signup, signout pages
- **Session management** working correctly
- **Type safety** fully implemented
- **Role-based access control** with 4 system roles (administrator, researcher, wizard, observer)
- **User profile management** with edit capabilities
- **Password change functionality** with validation
- **Admin interface** for user and role management
- **Authorization utilities** for client and server-side use
#### 4. User Interface Components (60%) 🚧
- **Authentication pages** complete (signin, signup, signout)
- **User profile management** interface complete
- **Admin dashboard** with user/role management complete
- **Role-based navigation** and access control
- **Responsive UI components** using shadcn/ui
- **Protected route displays** and unauthorized handling
#### 5. Project Structure (100%)
- T3 stack properly configured
- Environment variables setup
- Database connection with connection pooling
@@ -50,89 +72,80 @@ All major tRPC routers implemented:
### 🚧 Current Issues & Blockers
#### 1. Type Safety Issues (Priority: High)
#### 1. Advanced Authentication Features Complete ✅
- **Role-based access control** fully implemented
- **Admin user management** interface working
- **User profile editing** and password changes
- **Authorization middleware** protecting all routes
- **Session-based role checking** throughout app
- **Complete admin dashboard** for system management
#### 2. API Router Schema Alignment Complete ✅
**All routers properly aligned with database schema:**
**Trials Router:**
```typescript
// Current problem: Database context not properly typed
async function checkTrialAccess(
db: any, // ← Should be properly typed
userId: string,
trialId: string
) { ... }
// All fields correctly aligned:
startedAt: trials.startedAt, // ✅ Correctly using schema fields
completedAt: trials.completedAt, // ✅ Correctly using schema fields
duration: trials.duration, // ✅ Correctly using schema fields
```
**Root causes:**
- Database context using `any` type instead of proper Drizzle types
- Missing type imports for database operations
- Enum value mismatches between router expectations and schema
#### 2. Schema Field Mismatches (Priority: High)
Several routers reference fields that don't exist in the actual schema:
**Trials Router Issues:**
**Robots Router:**
```typescript
// Router expects:
startTime: trials.startTime, // ❌ Does not exist
endTime: trials.endTime, // ❌ Does not exist
completedSteps: trials.completedSteps, // ❌ Does not exist
// Schema actually has:
startedAt: trials.startedAt, // ✅ Exists
completedAt: trials.completedAt, // ✅ Exists
duration: trials.duration, // ✅ Exists
// All fields correctly aligned with schema:
id, name, manufacturer, model, description, capabilities,
communicationProtocol, createdAt, updatedAt // ✅ All exist in schema
```
**Robots Router Issues:**
**Participants Router:**
```typescript
// Router expects fields not in schema:
studyId, ipAddress, port, isActive, lastHeartbeat, trustLevel, type
// Correctly using schema fields:
participantCode: participants.participantCode, // ✅ Correctly aligned
email, name, demographics, consentGiven // ✅ All schema fields
```
**Participants Router Issues:**
#### 3. Type Safety Complete ✅
```typescript
// Router expects:
identifier: participants.identifier, // ❌ Does not exist
// Schema has:
participantCode: participants.participantCode, // ✅ Exists
```
#### 3. Enum Type Mismatches (Priority: Medium)
```typescript
// Current approach causes type errors:
inArray(studyMembers.role, ["owner", "researcher"] as any)
// Should use proper enum types from schema
// Proper enum usage throughout:
inArray(studyMembers.role, ["owner", "researcher"] as const) // ✅ Proper typing
// All database operations properly typed with Drizzle
```
### 🎯 Immediate Action Items
#### Phase 1: Fix Type Safety (Est: 2-4 hours)
1. **Update database context typing**
```typescript
// Fix in all routers:
import { db } from "~/server/db";
// Use ctx.db with proper typing instead of any
```
#### Phase 1: Complete Authentication System ✅ (Completed)
1. **Core Authentication**
- NextAuth.js v5 with email/password authentication
- JWT session strategy with role support
- Proper type safety throughout
2. **Fix enum usage**
```typescript
// Import and use actual enum values
import { studyMemberRoleEnum } from "~/server/db/schema";
inArray(studyMembers.role, ["owner", "researcher"] as const)
```
2. **Role-Based Access Control**
- 4 system roles: administrator, researcher, wizard, observer
- Role assignment and management via admin interface
- Authorization utilities for client and server-side
3. **Add proper error handling types**
3. **User Management**
- User profile management with edit capabilities
- Password change functionality with validation
- Admin dashboard for user and role management
#### Phase 2: Schema Alignment (Est: 3-6 hours)
1. **Audit all router field references against actual schema**
2. **Update router queries to use correct field names**
3. **Consider schema migrations if router expectations are more logical**
4. **Route Protection & UI**
- Middleware protecting all authenticated routes
- Complete authentication pages (signin, signup, signout)
- Admin interface with user table and role management
- Unauthorized access handling
#### Phase 3: Core Functionality Testing (Est: 4-8 hours)
1. **Set up local development environment**
2. **Create basic UI components for testing**
3. **Test each router endpoint**
4. **Validate database operations**
#### Phase 2: API Router Schema Alignment Complete ✅ (Completed)
1. **All router field references audited and aligned**
2. **All router queries using correct field names**
3. **Type safety verified across all database operations**
#### Phase 3: UI Implementation (Est: 4-8 hours) - Following Authentication
1. **Create study management interface**
2. **Build experiment designer components**
3. **Implement trial execution interface**
4. **Add data analysis components**
### 🏗️ Architecture Decisions Made
@@ -233,12 +246,29 @@ src/
| Component | Completion | Status | Priority |
|-----------|------------|--------|----------|
| Database Schema | 100% | ✅ Complete | - |
| API Routers | 95% | 🚧 Type fixes needed | High |
| Authentication | 90% | 🚧 Testing needed | High |
| UI Components | 0% | ❌ Not started | Medium |
| API Routers | 100% | ✅ Complete | - |
| Authentication | 100% | ✅ Complete | - |
| UI Components | 60% | 🚧 Auth & admin interfaces done | Medium |
| Trial Execution | 80% | 🚧 Integration needed | High |
| Real-time Features | 20% | ❌ WebSocket setup needed | Medium |
| File Upload | 70% | 🚧 R2 integration needed | Medium |
| Documentation | 85% | 🚧 API docs needed | Low |
The foundation is solid and most of the complex backend logic is implemented. The main blockers are type safety issues that can be resolved quickly, followed by building the frontend interface.
**Advanced authentication system with role-based access control is now complete!** This includes:
-**Full Authentication Flow**: Registration, login, logout, password changes
-**Role-Based Access Control**: 4 system roles with proper authorization
-**Admin Interface**: Complete user and role management dashboard
-**User Profile Management**: Edit profiles, change passwords, view roles
-**Route Protection**: Middleware-based authentication for all protected routes
-**UI Components**: Professional authentication and admin interfaces
**Complete API infrastructure with schema alignment is also finished!** This includes:
-**11 tRPC Routers**: All major functionality implemented and working
-**Schema Alignment**: All router queries properly reference existing database fields
-**Type Safety**: Full TypeScript coverage with proper Drizzle typing
-**Error Handling**: Comprehensive validation and error responses
-**Authorization**: Proper role-based access control throughout all endpoints
The backend foundation is robust and production-ready. Next priorities are building study/experiment management interfaces and real-time trial execution features.