// src/hooks.server.ts

import type { Handle } from '@sveltejs/kit';
import jwt from 'jsonwebtoken';

const SECRET_KEY = 'my_secret_key'; // 실제 운영 시 .env 파일에 보관

export const handle: Handle = async ({ event, resolve }) => {
  const token = event.cookies.get('session');

  if (token) {
    try {
      const user = jwt.verify(token, SECRET_KEY);
      event.locals.user = user;
    } catch (err) {
      event.locals.user = null;
    }
  } else {
    event.locals.user = null;
  }

  const response = await resolve(event);

  // ffmpeg.wasm 관련 헤더 유지
  response.headers.set('Cross-Origin-Opener-Policy', 'same-origin');
  response.headers.set('Cross-Origin-Embedder-Policy', 'require-corp');
  return response;
};

