loads fonts from AWS S3 when PUBLIC_SIERO_IMG_URL is set, otherwise falls back to local /fonts directory
82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
import type { Handle, HandleFetch } from '@sveltejs/kit'
|
|
import { sequence } from '@sveltejs/kit/hooks'
|
|
import { paraglideMiddleware } from '$lib/paraglide/server'
|
|
import { getAccountFromCookies, getUserFromCookies } from '$lib/auth/cookies'
|
|
import { PUBLIC_SIERO_API_URL } from '$env/static/public'
|
|
import { generateFontFaceCSS, getFontPreloadLinks } from '$lib/utils/fonts'
|
|
|
|
export const handleSession: Handle = async ({ event, resolve }) => {
|
|
const account = getAccountFromCookies(event.cookies)
|
|
const user = getUserFromCookies(event.cookies)
|
|
|
|
// Debug logging for auth issues
|
|
const allCookies = event.cookies.getAll()
|
|
console.log('[hooks.server] Request to:', event.url.pathname)
|
|
console.log('[hooks.server] All cookies:', allCookies.map(c => c.name))
|
|
|
|
if (account) {
|
|
console.log('[hooks.server] Account cookie found:', {
|
|
hasToken: !!account.token,
|
|
hasExpiresAt: !!account.expires_at,
|
|
username: account.username
|
|
})
|
|
} else {
|
|
console.log('[hooks.server] No account cookie found')
|
|
}
|
|
|
|
event.locals.session = {
|
|
account,
|
|
user,
|
|
isAuthenticated: Boolean(account?.token)
|
|
}
|
|
|
|
// Pass auth data for client-side auth store initialization
|
|
event.locals.auth = account?.token
|
|
? {
|
|
accessToken: account.token,
|
|
user: user,
|
|
expiresAt: account.expires_at ?? ''
|
|
}
|
|
: null
|
|
|
|
return resolve(event)
|
|
}
|
|
|
|
// Generate font CSS and preload links once at startup
|
|
const fontCSS = generateFontFaceCSS()
|
|
const fontPreloads = getFontPreloadLinks()
|
|
|
|
const handleParaglide: Handle = ({ event, resolve }) =>
|
|
paraglideMiddleware(event.request, ({ request, locale }) => {
|
|
event.request = request
|
|
|
|
return resolve(event, {
|
|
transformPageChunk: ({ html }) => {
|
|
// Inject font preloads and @font-face CSS into the head
|
|
const fontStyle = `<style id="font-faces">${fontCSS}</style>`
|
|
html = html.replace('</head>', `${fontPreloads}\n${fontStyle}\n</head>`)
|
|
return html.replace('%paraglide.lang%', locale)
|
|
}
|
|
})
|
|
})
|
|
|
|
export const handle: Handle = sequence(handleSession, handleParaglide)
|
|
|
|
const apiOrigin = new URL(PUBLIC_SIERO_API_URL || 'http://localhost:3000/api/v1').origin
|
|
|
|
export const handleFetch: HandleFetch = async ({ event, request, fetch }) => {
|
|
const url = new URL(request.url)
|
|
if (url.origin === apiOrigin) {
|
|
const token = event.locals.session?.account?.token
|
|
if (token) {
|
|
request = new Request(request, {
|
|
headers: new Headers({
|
|
...Object.fromEntries(request.headers),
|
|
authorization: `Bearer ${token}`
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
return fetch(request)
|
|
}
|