-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 유저정보 업데이트 시, 액세스토큰 발급으로 변경 (#71)
* refactor: 유저정보 업데이트 시, 액세스토큰 발급으로 변경 * feat: redis 도입 (#49) * feat: 리프래시 토큰 핸들링 (#48) * refactor: 중복 try catch 제거 (#48) * refactor: 리프래시 토큰 서명정보 삭제 -> payload가 아닌 key로만 비교하는 방법으로 변경 * refactor: jwt payload 타입 확장 선언으로 코드 개선 * refactor: redis connect 보장을 위한 then -> await으로 변경 Co-authored-by: scarf <[email protected]> * style: createclient 모듈만 사용 (테스트 커밋) * fix: 초기화 전 참조 에러 해결 contract를 받아 router를 작성해야하는데, 한 번에 빼내려고 contract에 router를 선언해버려서 참조를 하지 못하는 버그 발생. index 파일 만들어서 해결 * style: 포메팅 적용 * feat: openapi 항목 추가 * fix: RT_EXPIRED_BY_NUMBER 환경변수 기본값 추가 * refactor: header.user 보장, spread syntax 일부 적용 (#58) --------- Co-authored-by: scarf <[email protected]>
- Loading branch information
Showing
4 changed files
with
38 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,55 @@ | ||
import { initServer } from "@ts-rest/express"; | ||
import db from "../../db/models/index"; | ||
import { userInfoContract } from "./userInfo_contract"; | ||
import { createAccessToken } from "../authorization/jwtUtils"; | ||
import { UpdateCondition } from "../userSchema"; | ||
|
||
const s = initServer(); | ||
|
||
type UpdateCondition = { | ||
lat?: number; | ||
lon?: number; | ||
is_recommend_lunch?: boolean; | ||
}; | ||
|
||
export const userInfoRouter = s.router(userInfoContract, { | ||
//------------------------------------------------------------------------// | ||
getUserInfo: { | ||
handler: async ({ headers }) => { | ||
//유효성 검사덕에 header.user가 보장됨 | ||
const user = headers.user; | ||
|
||
const { userId: _userId, ...userInfo } = headers.user; | ||
return { | ||
status: 200, | ||
body: { | ||
lat: user?.lat, | ||
lon: user?.lon, | ||
isRecommendLunch: user?.isRecommendLunch, | ||
}, | ||
body: userInfo, | ||
}; | ||
}, | ||
}, | ||
//------------------------------------------------------------------------// | ||
updateUserInfo: { | ||
handler: async ({ headers, body }) => { | ||
//업데이트 후 바뀐 유저정보 고민 필요 | ||
const user = headers.user; | ||
|
||
let user = headers.user; | ||
let updateCondition: UpdateCondition = {}; | ||
|
||
if (body.isRecommendLunch) | ||
//요청 내용만 변경하기 위한 updateCondition | ||
if (body.isRecommendLunch != undefined) { | ||
updateCondition.is_recommend_lunch = body.isRecommendLunch; | ||
|
||
if (body.lat && body.lon) { | ||
updateCondition.lat = body.lat; | ||
updateCondition.lon = body.lon; | ||
} | ||
|
||
if (updateCondition) { | ||
const updateResult = await db.User.update(updateCondition, { | ||
where: { user_id: user?.userId }, | ||
}); | ||
if (updateResult[0]) | ||
return { | ||
status: 200, | ||
body: { message: "성공" }, | ||
}; | ||
const { isRecommendLunch: _isRecommendLunch, ...latlon } = body; | ||
updateCondition = { ...updateCondition, ...latlon }; | ||
console.log(updateCondition); | ||
|
||
if (Object.keys(updateCondition).length === 0) | ||
return { | ||
status: 400, | ||
body: { error: "변경사항 없음" }, | ||
body: { error: "잘못된 요청, 업데이트 정보 없음" }, | ||
}; | ||
|
||
try { | ||
await db.User.update(updateCondition, { | ||
where: { user_id: user.userId }, | ||
}); | ||
|
||
const tokenInfo = { ...user, ...body }; | ||
const newAccessToken = createAccessToken(tokenInfo); | ||
|
||
return { status: 200, body: { accessToken: newAccessToken } }; | ||
} catch { | ||
return { status: 404, body: { error: "유저정보 변경 실패" } }; | ||
} | ||
return { | ||
status: 404, | ||
body: { error: "업데이트 정보 없음" }, | ||
}; | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters