|
| 1 | + |
| 2 | +## 사용자 정보 관련하여 수정 |
| 3 | + |
| 4 | +### 내가 작성한 리뷰 목록 조회 API, 내가 진행 중인 미션 목록 조회 API |
| 5 | + |
| 6 | +path parameter (:userId) 제거 |
| 7 | + |
| 8 | +- 로그인한 사용자의 ID는 이미 isLogin 미들웨어를 통해 req.user에 담겨 있기 때문 |
| 9 | + |
| 10 | +controller 수정 |
| 11 | + |
| 12 | +- 기존 |
| 13 | + |
| 14 | + ```jsx |
| 15 | + export const handleListUserReviews = async (req, res, next) => { |
| 16 | + const reviews = await listUserReviews( parseInt(req.params.userId), typeof req.query.cursor === "string" ? parseInt(req.query.cursor) : 0 ); |
| 17 | + res.status(StatusCodes.OK).success(reviews); } |
| 18 | + }; |
| 19 | + ``` |
| 20 | + |
| 21 | +- 수정 |
| 22 | + |
| 23 | + 로그인한 사용자 id(req.user.id) 사용 |
| 24 | + |
| 25 | + ```jsx |
| 26 | + export const handleListUserReviews = async (req, res, next) => { |
| 27 | + try { |
| 28 | + const userId = req.user.id; |
| 29 | + const cursor = typeof req.query.cursor === "string" ? parseInt(req.query.cursor) : 0; |
| 30 | + |
| 31 | + const reviews = await listUserReviews(userId, cursor); |
| 32 | + |
| 33 | + res.status(StatusCodes.OK).success(reviews); |
| 34 | + } catch (err) { |
| 35 | + next(err); |
| 36 | + } |
| 37 | + }; |
| 38 | + ``` |
| 39 | + |
| 40 | + |
| 41 | +### 리뷰 추가 API, 가게의 미션을 도전 중인 미션에 추가 API |
| 42 | + |
| 43 | +controller 수정 |
| 44 | + |
| 45 | +- 기존 |
| 46 | + |
| 47 | + ```jsx |
| 48 | + try { |
| 49 | + const dto = createUserMissionDto(req.body); |
| 50 | + const userMission = await addUserMission(dto); |
| 51 | + res.status(StatusCodes.CREATED).success(userMission); |
| 52 | + } catch (err) { |
| 53 | + next(err); |
| 54 | + } |
| 55 | + ``` |
| 56 | + |
| 57 | +- 수정 |
| 58 | + |
| 59 | + 로그인한 사용자 id 사용 |
| 60 | + |
| 61 | + ```jsx |
| 62 | + try { |
| 63 | + const dto = { |
| 64 | + ...createUserMissionDto(req.body), |
| 65 | + userId: req.user.id, |
| 66 | + }; |
| 67 | + |
| 68 | + const userMission = await addUserMission(dto); |
| 69 | + res.status(StatusCodes.CREATED).success(userMission); |
| 70 | + } catch (err) { |
| 71 | + next(err); |
| 72 | + } |
| 73 | + ``` |
| 74 | + |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## 내 정보 수정 API |
| 79 | + |
| 80 | +`PATCH /api/v1/users/my` |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +성공 응답, 실패 응답(커스텀에러: 401로 처리) |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +실행 결과 |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +Postman GET /mypage 실행 |
| 97 | + |
| 98 | +내 정보 수정 API로 수정한 값으로 바뀐 것을 확인할 수 있다. |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## 기존 API에 JWT 적용하여 로그인한 사용자만 쓸 수 있도록 보호 |
| 103 | + |
| 104 | +### 미들웨어를 라우터에 적용 |
| 105 | + |
| 106 | +```jsx |
| 107 | +app.post("/api/v1/stores", isLogin, handleAddStore); |
| 108 | +... |
| 109 | +``` |
| 110 | + |
| 111 | +- isLogin 미들웨어를 로그인이 필요한 API 라우터에 적용 (`index.js`) |
| 112 | + |
| 113 | +### swagger |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +```jsx |
| 118 | +const doc = { |
| 119 | + info: { |
| 120 | + title: "UMC 9th", |
| 121 | + description: "UMC 9th Node.js 테스트 프로젝트입니다.", |
| 122 | + }, |
| 123 | + host: "localhost:3000", |
| 124 | + components: { |
| 125 | + securitySchemes: { |
| 126 | + BearerAuth: { |
| 127 | + type: "http", |
| 128 | + scheme: "bearer", |
| 129 | + bearerFormat: "JWT", |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }; |
| 134 | +``` |
| 135 | + |
| 136 | +- 기존 doc 객체에 `components.securitySchemes` 추가 (`index.js`) |
| 137 | +- 로그인이 필요한 API 주석에 `#swagger.security = [{ "BearerAuth": [] }]` 추가 (`controller.js`) |
0 commit comments