Skip to content

Commit

Permalink
fix: server hook auth handle and invalidate on login
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Feb 16, 2025
1 parent 946db0c commit c895973
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
41 changes: 23 additions & 18 deletions frontend/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import client from '$lib/clients';
import type { components } from '$lib/clients/v1/schema';
import type { Handle, HandleFetch } from '@sveltejs/kit';

type Profile = components['schemas']['Profile'];

export const handle: Handle = async ({ event, resolve }) => {
const auth_token = event.cookies.get('auth_token');
const auth_user_profile_id = event.cookies.get('auth_user_profile_id');
// init local
let profile: Profile | null = null;

if (auth_token && auth_user_profile_id) {
const { data, error, response } = await client.GET('/u/me/', {
headers: {
Authorization: `Bearer ${auth_token}`,
'Profile-Id': auth_user_profile_id.toString()
if (!event.locals.profile) {
const { data, error, response } = await client.GET('/u/me/', {
headers: {
Authorization: `Bearer ${auth_token}`,
'Profile-Id': auth_user_profile_id
}
});

if (response.ok && data) {
event.locals.profile = data;
} else if (error) {
console.error(error);
event.locals.profile = null;

event.cookies.delete('auth_token', { path: '/' });
event.cookies.delete('auth_user_profile_id', { path: '/' });
}
});

if (response.ok && data) {
profile = data;
} else if (error) {
console.error(error);
}
}

event.locals.profile = profile;
if (event.locals.profile && ['/login', '/register', '/password'].includes(event.url.pathname)) {
return new Response(null, {
status: 302,
headers: { location: '/' }
});
}

const response = await resolve(event);
return response;
return await resolve(event);
};

export const handleFetch: HandleFetch = async ({ event, request, fetch }) => {
Expand Down
7 changes: 2 additions & 5 deletions frontend/src/lib/components/header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,10 @@
/>
</div>
{:else}
<button
class="btn btn-primary h-10 px-3 text-sm font-bold"
onclick={() => modalsStore.open('auth')}
>
<a href="/login" class="btn btn-primary h-10 px-3 text-sm font-bold">
Join In!
<coreicons-shape-log-in class="size-4"></coreicons-shape-log-in>
</button>
</a>
{/if}
</div>
</header>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { goto, invalidateAll } from '$app/navigation';
import client from '$lib/clients/v1/client';
import Avatar from '$lib/components/ui/avatar.svelte';
import { PROFILE_CREATE_LIMIT } from '$lib/constants/limits';
Expand Down Expand Up @@ -46,7 +46,10 @@
if (res.ok) {
const data = await res.json();
if (data.success) goto('/');
if (!data.success) return;
goto('/');
await invalidateAll();
}
} catch (err) {
console.error(err);
Expand Down

0 comments on commit c895973

Please sign in to comment.