Skip to content

Commit

Permalink
feat: 🎸 hook up role-select demo to api endpoint and clerk
Browse files Browse the repository at this point in the history
  • Loading branch information
BigAB committed Nov 16, 2022
1 parent fa9e97a commit 1f151bf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const handle: Handle = async ({ event, resolve }) => {
const sessionToken = event.cookies.get('__session');
if (sessionToken) {
try {
const session = await verifySession();
const session = await verifySession(sessionToken);
if (session) {
event.locals.session = session;
}
Expand Down
14 changes: 12 additions & 2 deletions src/lib/components/RoleSelect.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<script lang="ts">
import { getClerkStore } from '$lib/clerk-svelte';
const clerk = getClerkStore();
let user = $clerk?.user;
let role = (user?.publicMetadata.role as string) || '';
$: role = (user?.publicMetadata.role as string) || '';
let loading = false;
const setRole = async (e: Event) => {
const target = e.target as HTMLSelectElement;
role = target.value;
loading = true;
await fetch('/api/updateRole', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ role: target.value }),
});
loading = false;
};
</script>

Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/updateRole/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export const POST: RequestHandler = async ({ locals, request }) => {
return json({ ok: false, error: 'Users Session not found' });
}
const body = await request.json();
users.updateUser(locals.session.userId, {
const user = await users.updateUser(locals.session.userId, {
publicMetadata: {
role: body.role,
},
});
return json({ ok: true });
return json({ ok: true, user });
};

0 comments on commit 1f151bf

Please sign in to comment.