index.ts
1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type { PathParams } from 'msw'
import { HttpResponse, http } from 'msw'
import { db } from '@db/auth/db'
import type { UserOut } from '@db/auth/types'
// Handlers for auth
export const handlerAuth = [
http.post<PathParams>(('/api/auth/login'), async ({ request }) => {
const { email, password } = await request.json() as { email: string; password: string }
let errors: Record<string, string[]> = {
email: ['Something went wrong'],
}
const user = db.users.find(u => u.email === email && u.password === password)
if (user) {
try {
const accessToken = db.userTokens[user.id]
// We are duplicating user here
const userData = { ...user }
const userOutData = Object.fromEntries(
Object.entries(userData)
.filter(
([key, _]) => !(key === 'password' || key === 'abilityRules'),
),
) as UserOut['userData']
const response: UserOut = {
userAbilityRules: userData.abilityRules,
accessToken,
userData: userOutData,
}
return HttpResponse.json(response,
{ status: 201 })
}
catch (e: unknown) {
errors = { email: [e as string] }
}
}
else {
errors = { email: ['Invalid email or password'] }
}
return HttpResponse.json({ errors }, { status: 400 })
}),
]