-
Notifications
You must be signed in to change notification settings - Fork 2
/
+server.ts
40 lines (32 loc) · 1.15 KB
/
+server.ts
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
import { USERFRONT_API_KEY } from '$env/static/private';
import { PUBLIC_USERFRONT_PUBLIC_KEY_BASE64 } from '$env/static/public';
import { verifyToken } from '$lib/index.js';
import { error, json } from '@sveltejs/kit';
export const PUT = async ({ request }) => {
const payload = await request.json();
// Read the JWT access token from the request header:
const authHeader = request.headers.get('authorization');
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
throw error(401, 'Missing authorization token.');
}
// Verify the token using the Userfront public key:
const auth = await verifyToken(PUBLIC_USERFRONT_PUBLIC_KEY_BASE64, token);
if (!auth) {
throw error(403, 'Error validating authorization token.');
}
const userId = auth.userId;
const response = await fetch(`https://api.userfront.com/v0/users/${userId}`, {
method: 'PUT',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${USERFRONT_API_KEY}`
},
body: JSON.stringify(payload)
});
console.log(response.status);
console.log(response.statusText);
const results = await response.json();
console.log(results);
return json(results);
};