Skip to content

Commit

Permalink
refactor: 유저정보 업데이트 시, 액세스토큰 발급으로 변경 (#71)
Browse files Browse the repository at this point in the history
* 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
Yonge2 and scarf005 authored Nov 8, 2023
1 parent b54692c commit 78949a1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
14 changes: 6 additions & 8 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { z } from "zod";

export const BaseHeadersSchema = z.object({
authorization: z.string(),
user: z
.object({
userId: z.string(),
lat: z.number(),
lon: z.number(),
isRecommendLunch: z.coerce.boolean(),
})
.optional(),
user: z.object({
userId: z.string(),
lat: z.number(),
lon: z.number(),
isRecommendLunch: z.coerce.boolean(),
}),
});
2 changes: 1 addition & 1 deletion src/user/userInfo/userInfo_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const userInfoContract = contract.router(
path: "/users",
responses: {
200: z.object({
message: z.string(),
accessToken: z.string(),
}),
},
body: UserSettingSchema,
Expand Down
61 changes: 25 additions & 36 deletions src/user/userInfo/userInfo_router.ts
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: "업데이트 정보 없음" },
};
},
},
});
6 changes: 6 additions & 0 deletions src/user/userSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ declare module "jsonwebtoken" {
isRecommendLunch: boolean;
}
}

export type UpdateCondition = {
lat?: number;
lon?: number;
is_recommend_lunch?: boolean;
};

0 comments on commit 78949a1

Please sign in to comment.