starfleet-ui
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#6 · main · 2 min 01 s
0Étoiles
0Forks
0Issues
| 1 | import { redirect } from '@sveltejs/kit'; |
| 2 | import { ApiError, get } from '$lib/server/api'; |
| 3 | import { clearToken } from '$lib/server/session'; |
| 4 | import type { Captain } from '$lib/types'; |
| 5 | import type { LayoutServerLoad } from './$types'; |
| 6 | |
| 7 | export const load: LayoutServerLoad = async ({ locals, cookies }) => { |
| 8 | if (!locals.token) return { captain: null }; |
| 9 | try { |
| 10 | const captain = await get<Captain>(locals.token, '/v1/captain'); |
| 11 | return { captain: captain.data }; |
| 12 | } catch (e) { |
| 13 | // Unknown or revoked key: forget it, or every page would fail the same way. |
| 14 | // This load runs on every page and before its children, so it catches this first. |
| 15 | if (e instanceof ApiError && e.status === 401) { |
| 16 | clearToken(cookies); |
| 17 | throw redirect(303, '/login?expired=1'); |
| 18 | } |
| 19 | throw e; |
| 20 | } |
| 21 | }; |