-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
32 lines (27 loc) · 738 Bytes
/
utils.js
File metadata and controls
32 lines (27 loc) · 738 Bytes
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
import { assertToken } from "./api.js";
export async function parseUser({ headers }) {
const asr = headers.get('Authorization')?.split(/\s+/g).pop();
if (!asr) {
throw respondWithError({
code: "Bad Request",
message: "Missing headers: Authorization"
}, 400);
}
const user = await assertToken(asr);
if (!user) {
throw respondWithError({
code: "Unauthorized",
message: "Authentication failed: invalid token"
}, 401);
}
return user
}
export function respondWithError(data, status = 400) {
return json({ error: data }, status)
}
export function json(data, status = 200) {
return new Response(JSON.stringify(Object.assign({ status }, data)), {
headers: { 'Content-Type': "application/json" },
status
})
}