diff --git a/src/common/swagger/index.js b/src/common/swagger/index.js index 49d12df..9f51e47 100644 --- a/src/common/swagger/index.js +++ b/src/common/swagger/index.js @@ -34,8 +34,12 @@ const swaggerDefinition = { }, servers: [ { - url: 'http://localhost:3000', - description: 'Development server', + url: process.env.NODE_ENV === 'production' + ? 'http://3.35.0.31:3000' + : 'http://localhost:3000', + description: process.env.NODE_ENV === 'production' + ? 'Production server' + : 'Development server', }, ], components: { diff --git a/src/common/swagger/token.json b/src/common/swagger/token.json new file mode 100644 index 0000000..f5ebd70 --- /dev/null +++ b/src/common/swagger/token.json @@ -0,0 +1,33 @@ +{ + "paths": { + "/api/token": { + "get": { + "tags": ["Token"], + "summary": "테스트용 임시 토큰 발급", + "description": "userId 1번 회원의 JWT 토큰을 발급합니다.", + "responses": { + "200": { + "description": "토큰 발급 성공", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "token": { + "type": "string", + "example": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsIm5pY2tuYW1lIjoi7YWM7Iqk7Yq466mEIiwiYWNjb3VudElkIjoxLCJwcm92aWRlciI6Imdvb2dsZSIsImlhdCI6MTY5MTY3MjA5NCwiZXhwIjoxNjkxNjcyNjk0fQ.abc123def456" + }, + "userId": { + "type": "integer", + "example": 1 + } + } + } + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/routes.js b/src/routes.js index 9759750..6840840 100644 --- a/src/routes.js +++ b/src/routes.js @@ -10,6 +10,7 @@ import paymentRouter from "./payment/payment.routes.js" import pointRouter from "./point/point.routes.js" import requestRouter from "./request/request.routes.js" import homeRouter from "./home/home.routes.js" +import tokenRouter from "./token.routes.js" const router = express.Router(); @@ -24,6 +25,7 @@ router.use("/payments", paymentRouter); router.use("/points", pointRouter); router.use("/requests", requestRouter); router.use("/home", homeRouter); +router.use("/", tokenRouter); export default router; \ No newline at end of file diff --git a/src/token.routes.js b/src/token.routes.js new file mode 100644 index 0000000..d951b69 --- /dev/null +++ b/src/token.routes.js @@ -0,0 +1,41 @@ +import { Router } from 'express'; +import { PrismaClient } from '@prisma/client'; +import { signJwt } from './jwt.config.js'; +import { StatusCodes } from 'http-status-codes'; +import { parseWithBigInt, stringifyWithBigInt } from './bigintJson.js'; + +const router = Router(); +const prisma = new PrismaClient(); + +router.get('/token', async (req, res, next) => { + try { + const user = await prisma.user.findUnique({ + where: { id: 1 }, + include: { + account: true + } + }); + + const payload = { + userId: user.id.toString(), + nickname: user.nickname, + accountId: user.accountId.toString(), + provider: user.account.provider + }; + + const token = signJwt(payload); + + const result = { + token, + userId: user.id + }; + + const responseData = parseWithBigInt(stringifyWithBigInt(result)); + + res.status(StatusCodes.OK).success(responseData); + } catch (err) { + next(err); + } +}); + +export default router;