Skip to content

Commit

Permalink
svelte - isAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
while1618 committed Jan 6, 2024
1 parent b8a4609 commit 4a3dc5d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
5 changes: 1 addition & 4 deletions frontend/svelte-kit/src/lib/components/user-navbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import SunIcon from '$lib/icons/sun.svelte';
import avatar from '$lib/images/avatar.jpg';
import logo from '$lib/images/logo.png';
// import { RoleName } from '$lib/models/user/role';
// import { userStore } from '$lib/stores/user';
// $: isAdmin = $userStore?.roles.some((role) => role.name === RoleName.ADMIN);
$: isAdmin = true;
export let isAdmin: boolean;
</script>

<nav class="navbar bg-base-100 px-5">
Expand Down
7 changes: 7 additions & 0 deletions frontend/svelte-kit/src/lib/server/utils/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { JwtPayload } from '$lib/models/auth/jwt-payload';
import { RoleName } from '$lib/models/user/role';
import type { Cookies } from '@sveltejs/kit';
import jwt from 'jsonwebtoken';

Expand Down Expand Up @@ -37,3 +38,9 @@ export function removeAuth(cookies: Cookies, locals: App.Locals): void {
cookies.delete('refreshToken', { path: '/' });
locals.userId = null;
}

export function isAdmin(accessToken: string | undefined): boolean {
if (!accessToken) return false;
const { roles } = jwt.decode(accessToken) as JwtPayload;
return roles?.includes(RoleName.ADMIN) ?? false;
}
7 changes: 4 additions & 3 deletions frontend/svelte-kit/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { UserDTO } from '$lib/models/user/user';
import { makeRequest } from '$lib/server/apis/api';
import { HttpRequest, removeAuth } from '$lib/server/utils/util';
import { HttpRequest, isAdmin, removeAuth } from '$lib/server/utils/util';
import { error, redirect, type NumericRange } from '@sveltejs/kit';
import type { LayoutServerLoad } from './$types';

export const load = (async ({ locals, cookies }) => {
if (!locals.userId) return { user: null };
const accessToken = cookies.get('accessToken');

const response = await makeRequest({
method: HttpRequest.GET,
path: `/profile`,
auth: cookies.get('accessToken'),
auth: accessToken,
});

if ('error' in response) {
Expand All @@ -21,5 +22,5 @@ export const load = (async ({ locals, cookies }) => {
error(response.status as NumericRange<400, 599>, { message: response.error });
}

return { user: response as UserDTO };
return { user: response as UserDTO, isAdmin: isAdmin(accessToken) };
}) satisfies LayoutServerLoad;
2 changes: 1 addition & 1 deletion frontend/svelte-kit/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<div class="flex h-screen flex-col overflow-hidden bg-base-200">
{#if data.user}
<UserNavbar />
<UserNavbar isAdmin={data.isAdmin} />
{:else}
<GuestNavbar />
{/if}
Expand Down

0 comments on commit 4a3dc5d

Please sign in to comment.