Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnnsu committed Apr 25, 2024
2 parents dfcc27a + 548a0a6 commit d891ed5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class AuthController {
summary: '회원가입 이메일 인증',
})
async confirmSignup(@Body() confirmSignupDto: ConfirmSignupDto) {
return await this.authService.confirmSignUp(confirmSignupDto);
await this.authService.confirmSignUp(confirmSignupDto);
}

// 인증번호 다시보내기
Expand Down
6 changes: 5 additions & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class AuthService {
};

try {
return await this.cognitoClient.signUp(params).promise();
this.logger.info(`User ${signupDto.email} signed up successfully.`);
await this.cognitoClient.signUp(params).promise();

return await this.userService.createUser(signupDto);
} catch (e) {
Expand Down Expand Up @@ -167,6 +168,7 @@ export class AuthService {
}
}

// 토큰 얻기
async getToken(refreshToken: string) {
const params = {
AuthFlow: 'REFRESH_TOKEN_AUTH',
Expand All @@ -190,6 +192,7 @@ export class AuthService {
}
}

// 비밀번호 잊었을 때 이메일로 코드요청
async forgotPassword(emailDto: EmailDto) {
const { email } = emailDto;
const params = {
Expand All @@ -210,6 +213,7 @@ export class AuthService {
}
}

// 새 비밀번호로 변경
async confirmPasswordReset(forgotPasswordDto: ForgotPasswordDto) {
const { email, code, newPassword } = forgotPasswordDto;

Expand Down
30 changes: 30 additions & 0 deletions src/auth/jwt-auth-guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AuthService } from './auth.service';
import {
CanActivate,
ExecutionContext,
Injectable,
UnauthorizedException,
Request,
} from '@nestjs/common';

@Injectable()
export class JwtAuthGuard implements CanActivate {
constructor(private readonly authService: AuthService) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<Request>();
const accessToken = request.headers['authorization']?.split(' ')[1];

if (!accessToken) {
throw new UnauthorizedException('헤더에 토큰이 없습니다.');
}

const user = await this.authService.tokenGetUser(accessToken);
if (!user || !user.email) {
throw new UnauthorizedException('유저가 없습니다.');
}

request['userId'] = user.id;
return true;
}
}
29 changes: 15 additions & 14 deletions src/auth/password.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Body, Controller, Post, Request, UseGuards } from '@nestjs/common';
import {
Body,
Controller,
HttpException,
HttpStatus,
Post,
Request,
} from '@nestjs/common';
import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
ApiBearerAuth,
ApiBody,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { ForgotSchema } from './schema/forgot.schema';
import { ForgotConfirmSchema } from './schema/forgotConfirm.schema';
Expand All @@ -18,6 +17,7 @@ import {
import { EmailDto } from './../dto/email.dto';
import { ForgotPasswordDto } from './../dto/forgotPassword.dto';
import { ChangePasswordDto } from './../dto/changePassword.dto';
import { JwtAuthGuard } from './jwt-auth-guard';

@Controller('api/user/v1/')
export class PasswordController {
Expand Down Expand Up @@ -56,6 +56,8 @@ export class PasswordController {
// 비밀번호 변경
@ApiTags('auth')
@Post('user/password')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('accessToken')
@ApiOperation({
summary: '비밀번호 변경',
})
Expand All @@ -65,11 +67,10 @@ export class PasswordController {
@Request() request,
@Body() changePasswordDto: ChangePasswordDto,
) {
const [type, token] = request.headers.authorization?.split(' ') ?? [];
if (type !== 'Bearer' || !token) {
throw new HttpException('토큰이 없습니다.', HttpStatus.BAD_REQUEST);
}

return await this.authService.changePassword(token, changePasswordDto);
const accessToken = request.headers['authorization']?.split(' ')[1];
return await this.authService.changePassword(
accessToken,
changePasswordDto,
);
}
}
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ async function bootstrap() {
.setDescription('pqsoft')
.setVersion('1.0')
.addTag('auth')
.addBearerAuth(
{ type: 'http', scheme: 'bearer', bearerFormat: 'JWT' },
'accessToken', // 이 이름이 스웨거 UI에서 보안 스키마를 참조하는 데 사용됩니다.
)
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api/user/v1', app, document);
Expand Down

0 comments on commit d891ed5

Please sign in to comment.