Skip to content
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

Refactor: Add error case #47

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6분?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로컬에서 테스트할때만 6분으로 해놔쓰

};
}
return {
httpOnly: true,
sameSite: 'none',
secure: true,
path: '/',
expires: new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 1 * 365),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ho-s 그 외 환경(stage, prod)에서는 길고긴 만료시간!!

domain: '.korrk.kr',
};
};
Loading