All files / src/routes +layout.server.ts

100% Statements 10/10
100% Branches 7/7
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      1x 3x   3x 3x 2x 2x   2x             1x 1x       2x    
import type { LayoutServerLoad } from './$types';
import { redirect } from '@sveltejs/kit';
 
export const load: LayoutServerLoad = async ({ locals: { supabase, safeGetSession }, url }) => {
	const { session, user } = await safeGetSession();
 
	let athlete = null;
	if (user) {
		const { data } = await supabase.from('athletes').select('*').eq('id', user.id).single();
		athlete = data;
 
		if (!athlete && url.pathname !== '/login' && url.pathname !== '/reset-password') {
			// NOTE: This exception list must stay in sync with hooks.server.ts isAuthRoute.
			// User exists (email confirmed, session valid) but no athlete row.
			// This means the DB trigger that creates the athlete on email confirmation
			// failed or hasn't fired yet. Sign the user out and redirect to login so
			// they don't get stuck in a broken state. This is a defensive guard — in
			// normal operation the trigger always creates the row.
			await supabase.auth.signOut();
			redirect(303, '/login');
		}
	}
 
	return { session, user, athlete };
};