import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import jwt from 'jsonwebtoken';

const SECRET = process.env.JWT_SECRET || 'changeme';

export const GET: RequestHandler = async ({ request }) => {
	const auth = request.headers.get('authorization');
	if (!auth) throw error(401, '토큰 없음');
	const token = auth.split(' ')[1];
	try {
		const user = jwt.verify(token, SECRET);
		return json(user);
	} catch {
		throw error(401, '유효하지 않은 토큰');
	}
};
