-
Notifications
You must be signed in to change notification settings - Fork 11
Express 임경민 미션 10 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: express-임경민
Are you sure you want to change the base?
Express 임경민 미션 10 #10
The head ref may contain hidden characters: "express-\uC784\uACBD\uBBFC"
Conversation
| const { email, nickname, password } = req.body; | ||
|
|
||
| if (!email || !emailRegex.test(email)) { | ||
| throw new BadRequestException('유효한 이메일 형식이 아닙니다.'); | ||
| } | ||
| if (!nickname) { | ||
| throw new BadRequestException('이름을 입력해주세요.'); | ||
| } | ||
| if (!password || password.length < 6) { | ||
| throw new BadRequestException('비밀번호는 6자 이상이어야 합니다.'); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zod를 사용하지 않고 있었다면 그냥 넘겼을 것 같은 부분인데, 현재 프로젝트에서 이미 zod를 사용하고 있으니 검증 로직을 전부 zod 스키마에서 수행하게 만들면 좋을 것 같네요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
데이터 검증은 zod로 처리하고 zod에서 발생하는 error를 캐치하는 핸들러를 하나 만들어서 express 미들웨어로 씌우거나 하는 방식으로 처리할 수도 있을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
빈 파일은 아직 구현이 안 된 부분인 걸까요?
| const page = parseInt(req.query.page, 10) || 1; | ||
| const pageSize = parseInt(req.query.pageSize, 10) || 10; | ||
| const pageSize = | ||
| parseInt(req.query.pageSize, 10) || Number.MAX_SAFE_INTEGER; | ||
| const keyword = req.query.keyword; | ||
| const orderBy = req.query.orderBy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req.query에서 데이터를 가져오는 부분도 zod로 처리하면 어떨까요?
| try { | ||
| const { authorization } = req.cookies; | ||
| if (!authorization) { | ||
| throw new UnauthorizedException('인증 정보가 없습니다.'); | ||
| } | ||
| const [tokenType, token] = authorization.split(' '); | ||
| if (tokenType !== 'Bearer' || !token) { | ||
| throw new UnauthorizedException('지원하지 않는 인증 방식입니다.'); | ||
| } | ||
|
|
||
| let decoded = verifyToken(token); | ||
|
|
||
| // Access Token이 만료된 경우 | ||
| if (!decoded) { | ||
| const { refreshToken: refreshTokenWithBearer } = req.cookies; | ||
| if (!refreshTokenWithBearer) { | ||
| throw new UnauthorizedException('인증 정보가 만료되었습니다.'); | ||
| } | ||
|
|
||
| const [refreshTokenType, refreshToken] = | ||
| refreshTokenWithBearer.split(' '); | ||
| if (refreshTokenType !== 'Bearer' || !refreshToken) { | ||
| throw new UnauthorizedException('지원하지 않는 인증 방식입니다.'); | ||
| } | ||
|
|
||
| const decodedRefreshToken = verifyToken(refreshToken); | ||
| if (!decodedRefreshToken) { | ||
| throw new UnauthorizedException('인증 정보가 만료되었습니다.'); | ||
| } | ||
|
|
||
| const user = await usersRepository.findUserById( | ||
| decodedRefreshToken.userId, | ||
| ); | ||
| if (!user || user.refreshToken !== refreshToken) { | ||
| throw new UnauthorizedException('인증 정보가 유효하지 않습니다.'); | ||
| } | ||
|
|
||
| // 새로운 Access Token 발급 | ||
| const newAccessToken = jwt.sign({ userId: user.id }, config.JWT_SECRET, { | ||
| expiresIn: '6h', | ||
| }); | ||
|
|
||
| res.cookie('authorization', `Bearer ${newAccessToken}`); | ||
| decoded = verifyToken(newAccessToken); | ||
| } | ||
|
|
||
| const user = await usersRepository.findUserById(decoded.userId); | ||
| if (!user) { | ||
| throw new UnauthorizedException( | ||
| '인증 정보와 일치하는 사용자가 없습니다.', | ||
| ); | ||
| } | ||
| req.user = user; | ||
| next(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
| function Grogu() {} | ||
|
|
||
| Grogu(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ 이 함수의 정체는 무엇인가요?
| // Refresh Token을 DB에 저장 | ||
| await usersRepository.updateUserById(user.id, { refreshToken }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
권한 회수를 목적으로 refresh token을 db에 저장하는 경우 refresh token은 user 모델에 넣어두기보다는 다중 기기 사용 등의 시나리오에 대응하기 위해 별도의 모델(테이블)로 분리하는 것이 좋습니다.
e.g. PC와 모바일에서 동시에 로그인을 한다면?
요구사항
백엔드 구현 요구사항
상품 등록
상품 상세
좋아요 기능
에러 처리
인증
상품 기능 인가
게시글 기능 인가
댓글 기능 인가
심화 요구사항
상태코드 (웹 API 관련)
인증