206 lines
5.6 KiB
TypeScript
206 lines
5.6 KiB
TypeScript
/**
|
|
* MSW handlers for auth endpoints
|
|
*/
|
|
|
|
import { http, HttpResponse } from 'msw';
|
|
|
|
export const handlersAuth = [
|
|
http.post('*/api/v1/auth/login', async () => {
|
|
return HttpResponse.json({
|
|
user: {
|
|
id: 1,
|
|
username: 'StorybookUser',
|
|
email: 'user@example.com',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
avatar_url: 'https://i.pravatar.cc/150?u=1',
|
|
},
|
|
token: {
|
|
access_token: 'mock_access_token_generic',
|
|
refresh_token: 'mock_refresh_token_generic',
|
|
expires_in: 3600,
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/login/2fa', async ({ request }) => {
|
|
const body = (await request.json()) as {
|
|
email?: string;
|
|
password?: string;
|
|
code?: string;
|
|
};
|
|
if (!body?.code || body.code.length < 6) {
|
|
return HttpResponse.json(
|
|
{ success: false, error: { code: 401, message: 'Invalid 2FA code' } },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
return HttpResponse.json({
|
|
user: {
|
|
id: 1,
|
|
username: 'User2FA',
|
|
email: body.email ?? 'user@example.com',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
avatar_url: null,
|
|
},
|
|
token: {
|
|
access_token: 'mock_2fa_token',
|
|
refresh_token: 'mock_2fa_refresh',
|
|
expires_in: 3600,
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/refresh', async () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: {
|
|
access_token: 'new_access_token_123',
|
|
refresh_token: 'new_refresh_token_123',
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.get('*/api/v1/auth/me', () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: {
|
|
id: 1,
|
|
username: 'StorybookUser',
|
|
email: 'user@example.com',
|
|
first_name: 'Story',
|
|
last_name: 'User',
|
|
avatar_url: 'https://i.pravatar.cc/150?u=1',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
updated_at: '2024-01-01T00:00:00Z',
|
|
role: 'user',
|
|
is_verified: true,
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/register', async () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: {
|
|
access_token: 'mock_access_token_register',
|
|
refresh_token: 'mock_refresh_token_register',
|
|
user: {
|
|
id: 1,
|
|
username: 'StorybookUser',
|
|
email: 'user@example.com',
|
|
created_at: '2024-01-01T00:00:00Z',
|
|
},
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/logout', () => {
|
|
return HttpResponse.json({ success: true });
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/password/reset-request', () => {
|
|
return HttpResponse.json({ success: true, message: 'Reset email sent' });
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/password/reset', () => {
|
|
return HttpResponse.json({ success: true, message: 'Password reset successful' });
|
|
}),
|
|
|
|
http.get('*/api/v1/auth/check-username', () => {
|
|
return HttpResponse.json({ success: true, available: true });
|
|
}),
|
|
|
|
http.get('*/api/v1/auth/oauth/providers', () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: {
|
|
providers: [
|
|
{ id: 'google', name: 'Google', authorizeUrl: '/api/v1/auth/oauth/google', icon: 'google' },
|
|
{ id: 'github', name: 'GitHub', authorizeUrl: '/api/v1/auth/oauth/github', icon: 'github' },
|
|
{ id: 'discord', name: 'Discord', authorizeUrl: '/api/v1/auth/oauth/discord', icon: 'discord' },
|
|
{ id: 'spotify', name: 'Spotify', authorizeUrl: '/api/v1/auth/oauth/spotify', icon: 'spotify' },
|
|
],
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/verify-email', () => {
|
|
return HttpResponse.json({ success: true, message: 'Email verified' });
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/resend-verification', () => {
|
|
return HttpResponse.json({ success: true, message: 'Verification email resent' });
|
|
}),
|
|
|
|
http.get('*/api/v1/auth/2fa/status', () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: { enabled: false, method: null },
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/2fa/setup', () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: {
|
|
secret: 'MOCKSECRET123',
|
|
qr_code: 'data:image/png;base64,mockqrcode',
|
|
backup_codes: ['CODE1', 'CODE2'],
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/2fa/verify', () => {
|
|
return HttpResponse.json({ success: true });
|
|
}),
|
|
|
|
http.post('*/api/v1/auth/2fa/disable', () => {
|
|
return HttpResponse.json({ success: true });
|
|
}),
|
|
|
|
http.get('*/api/v1/sessions', () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: {
|
|
sessions: [
|
|
{
|
|
id: 'session-1',
|
|
ip_address: '192.168.1.1',
|
|
user_agent:
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
|
|
created_at: '2024-01-05T10:00:00Z',
|
|
last_activity: '2024-01-05T12:00:00Z',
|
|
is_current: true,
|
|
},
|
|
{
|
|
id: 'session-2',
|
|
ip_address: '192.168.1.2',
|
|
user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0',
|
|
created_at: '2024-01-04T08:00:00Z',
|
|
last_activity: '2024-01-04T08:00:00Z',
|
|
is_current: false,
|
|
},
|
|
],
|
|
count: 2,
|
|
},
|
|
});
|
|
}),
|
|
|
|
http.post('*/api/v1/sessions/logout-others', () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: { message: 'Other sessions revoked successfully', sessions_revoked: 1 },
|
|
});
|
|
}),
|
|
|
|
http.delete('*/api/v1/sessions/*', () => {
|
|
return HttpResponse.json({ success: true, data: { message: 'ok' } });
|
|
}),
|
|
|
|
http.get('*/api/v1/auth/2fa', () => {
|
|
return HttpResponse.json({
|
|
success: true,
|
|
data: { enabled: false, method: null },
|
|
});
|
|
}),
|
|
];
|