Skip to content

Commit

Permalink
fix: better types
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewhrit committed Jan 20, 2023
1 parent e7e81a1 commit 082972d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// for information about these interfaces
// and what to do when importing types
declare namespace App {
// interface Locals {}
interface Locals {
token: string;
authenticated: boolean;
}
// interface PageData {}
// interface Error {}
// interface Platform {}
Expand Down
34 changes: 12 additions & 22 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import type { Handle } from '@sveltejs/kit';

export const handle = (async ({ event, resolve }) => {
const user = {
authenticated: false,
token: '',
};
// Check if the user is authenticated by seeing if the token cookie exists (truthy). In some
// cases, it can also be a string with the content undefined, so we also check for that.
const token = event.cookies.get('token');

// Check if user is authenticated by seeing if a token is stored in the cookies
const session = event.cookies.get('token');
// @TODO Check token against Eve

// @todo Check if the token is in a valid format
// Maybe also actually check if the user is validated with a request to Eve?

if (session !== undefined && session !== 'undefined') {
user.authenticated = true;
user.token = session;
if (!token && token !== 'undefined') {
// Session is undefined
event.locals.token = '';
event.locals.authenticated = false;
} else {
user.authenticated = false;
user.token = '';
// User is authenticated
event.locals.token = token;
event.locals.authenticated = true;
}

const response = await resolve({
...event,
locals: {
user,
},
});

return response;
return await resolve(event);
}) satisfies Handle;
1 change: 0 additions & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { LayoutServerLoad } from './$types';

export const load = (({ locals }) => {
return {
// @ts-expect-error defined
authenticated: locals.authenticated,
};
}) satisfies LayoutServerLoad;
5 changes: 2 additions & 3 deletions src/routes/me/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ async function getContainers(token: string) {
}

export const load = (async ({ parent, locals }) => {
// @ts-expect-error authenticated is defined in hooks.server.ts
// @ts-expect-error authenticated is defined in `routes/+layout.server.ts`
const { authenticated } = await parent();
// @ts-expect-error defined
const token: string = locals.user.token;
const token = (locals as { token: string }).token;

if (!authenticated) {
throw redirect(307, '/');
Expand Down
3 changes: 1 addition & 2 deletions src/routes/me/[uuid]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import type { PageServerLoad } from './$types';
export const load = (async ({ params, locals }) => {
const req = await fetch(`http://10.10.9.4:3000/virtual_machines/${params.uuid}`, {
headers: {
// @ts-expect-error user is defined in hooks.server.ts
Authorization: `Bearer ${locals.user.token}`,
Authorization: `Bearer ${locals.token}`,
},
});

Expand Down

0 comments on commit 082972d

Please sign in to comment.