Skip to content

Commit

Permalink
Refactor: Add error case (#47)
Browse files Browse the repository at this point in the history
- 닉네임이 설정되지 않은 유저일 때, 에러케이스 추가
  • Loading branch information
sally0226 authored Aug 13, 2024
1 parent d3aa896 commit 0c1460b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Response } from 'express';

import { KakaoInfo } from 'src/common/decorators/kakao-info.decorator';
import { KakaoGuard } from 'src/common/guards/kakao.guard';
import { getCookieOption } from 'src/common/helper/cookie.helper';
import { UserProvider } from 'src/entities';

import { AuthService } from './auth.service';
Expand All @@ -32,14 +33,11 @@ export class AuthController {
kakaoRefreshToken: refreshToken,
});

res.cookie('Authorization', 'Bearer ' + user.accessToken, {
httpOnly: true,
sameSite: 'none',
secure: true,
path: '/',
expires: new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 1 * 365),
domain: '.korrk.kr',
});
res.cookie(
'Authorization',
'Bearer ' + user.accessToken,
getCookieOption(),
);

return res.redirect(302, this.configService.get('CLIENT_URL'));
}
Expand Down
11 changes: 9 additions & 2 deletions src/auth/strategies/jwt.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import {
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';

import { ExtractJwt, Strategy, VerifiedCallback } from 'passport-jwt';

import { User } from 'src/entities/index';
import { UserService } from 'src/user/user.service';

@Injectable()
Expand All @@ -19,12 +24,14 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
}

async validate(payload: { id: number }, done: VerifiedCallback) {
const user = await this.userService.findOne({
const user: User = await this.userService.findOne({
id: payload.id,
});
if (!user) {
throw new UnauthorizedException('존재하지 않는 유저입니다.');
}
if (user.nickname == null)
throw new NotFoundException('닉네임 설정을 완료해주세요.');
done(null, user);
}
}
21 changes: 21 additions & 0 deletions src/common/helper/cookie.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CookieOptions } from 'express';

import { getNodeEnv } from 'src/common/helper/env.helper';
import { NODE_ENVIRONMENT } from 'src/common/helper/env.validation';

export const getCookieOption = (): CookieOptions => {
if (getNodeEnv === NODE_ENVIRONMENT.development) {
return {
path: '/',
maxAge: 360000,
};
}
return {
httpOnly: true,
sameSite: 'none',
secure: true,
path: '/',
expires: new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 1 * 365),
domain: '.korrk.kr',
};
};

0 comments on commit 0c1460b

Please sign in to comment.