71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import { http, HttpResponse } from 'msw'
|
|
|
|
import { MOCK_USERS } from './data'
|
|
|
|
/**
|
|
* Auth API Mock Handlers
|
|
*
|
|
* Mocks authentication endpoints for development and testing:
|
|
* - POST /auth/login - User login
|
|
* - POST /auth/logout - User logout
|
|
* - GET /auth/me - Get authenticated user
|
|
* - POST /auth/register - User registration
|
|
*
|
|
* Uses wildcard (*) to match any host for cross-environment compatibility
|
|
*/
|
|
export const authHandlers = [
|
|
// Get authenticated user (SSO session check)
|
|
// Returns { authenticated: boolean, user?: User } format expected by SSOClient
|
|
http.get('*/auth/me', () => HttpResponse.json(
|
|
{
|
|
authenticated: true,
|
|
user: MOCK_USERS[0],
|
|
},
|
|
{ status: 200 }
|
|
)),
|
|
|
|
// Login
|
|
http.post('*/auth/login', async ({ request }) => {
|
|
const body = await request.json() as { email: string; password: string }
|
|
const user = MOCK_USERS.find((u) => u.email === body.email)
|
|
|
|
if (!user) {
|
|
return HttpResponse.json(
|
|
{ error: 'Invalid credentials' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
return HttpResponse.json(user, { status: 200 })
|
|
}),
|
|
|
|
// Logout
|
|
http.post('*/auth/logout', () => HttpResponse.json({ success: true }, { status: 200 })),
|
|
|
|
// Register
|
|
http.post('*/auth/register', async ({ request }) => {
|
|
const body = await request.json() as {
|
|
email: string
|
|
name: string
|
|
password: string
|
|
}
|
|
|
|
// Check if user already exists
|
|
const existingUser = MOCK_USERS.find((u) => u.email === body.email)
|
|
if (existingUser) {
|
|
return HttpResponse.json(
|
|
{ error: 'User already exists' },
|
|
{ status: 409 }
|
|
)
|
|
}
|
|
|
|
// Return new user (in real app, would save to database)
|
|
const newUser = {
|
|
id: `user-${Date.now()}`,
|
|
email: body.email,
|
|
name: body.name,
|
|
}
|
|
|
|
return HttpResponse.json(newUser, { status: 201 })
|
|
}),
|
|
]
|