index.ts (1216B)
1 import { ERR_INVALID_REQUEST, ERR_METHOD_NOT_ALLOWED, ERR_NOT_FOUND, ERR_UNAUTHORIZED } from '@/lib/apierror' 2 import { getAccessTokenCookieName } from '@/lib/env' 3 import { getUserProfile } from '@/lib/models/user_profile' 4 import { getUserIdFromAccessToken } from '@/lib/security/token' 5 import { NextApiRequest, NextApiResponse } from 'next' 6 7 export default async function handler ( 8 req: NextApiRequest, 9 res: NextApiResponse, 10 ) { 11 if (req.method !== 'GET') { 12 res.status(405).json(ERR_METHOD_NOT_ALLOWED) 13 return 14 } 15 16 let id: number | typeof req.query.id = req.query.id 17 if (typeof id !== 'string') { 18 res.status(400).json(ERR_INVALID_REQUEST) 19 return 20 } 21 22 // id가 me일 경우, 현재 로그인한 사용자로 대체 23 if (id == 'me') { 24 const currentUserId = await getUserIdFromAccessToken(req.cookies[getAccessTokenCookieName()]) 25 if (currentUserId == null) { 26 res.status(401).json(ERR_UNAUTHORIZED) 27 return 28 } 29 id = currentUserId 30 } else { 31 id = parseInt(id, 10) 32 } 33 34 // 사용자 정보를 받아옴 35 const profile = await getUserProfile([id]) 36 if (profile == null) { 37 res.status(404).json(ERR_NOT_FOUND) 38 return 39 } 40 41 res.status(200).json(profile) 42 }