From e8969c7ad3eacccb22ef6f7c6286b5fc7d32a259 Mon Sep 17 00:00:00 2001 From: jinddings Date: Mon, 11 Nov 2024 17:01:06 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=94=A7=20fix=20:=20lint=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/app.module.ts | 1 - BE/src/auth/dto/auth-credentials.dto.ts | 17 ----------------- BE/src/auth/strategy/jwt.strategy.ts | 2 +- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/BE/src/app.module.ts b/BE/src/app.module.ts index 639c4d3d..8a1dab3d 100644 --- a/BE/src/app.module.ts +++ b/BE/src/app.module.ts @@ -5,7 +5,6 @@ import { ScheduleModule } from '@nestjs/schedule'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { AuthModule } from './auth/auth.module'; -import { User } from './auth/user.entity'; import { StockIndexModule } from './stock/index/stock-index.module'; import { StockTopfiveModule } from './stock/topfive/stock-topfive.module'; import { KoreaInvestmentModule } from './koreaInvestment/korea-investment.module'; diff --git a/BE/src/auth/dto/auth-credentials.dto.ts b/BE/src/auth/dto/auth-credentials.dto.ts index de6420bf..31bc69b6 100644 --- a/BE/src/auth/dto/auth-credentials.dto.ts +++ b/BE/src/auth/dto/auth-credentials.dto.ts @@ -5,7 +5,6 @@ import { MinLength, IsOptional, } from 'class-validator'; -import { IsString, Matches, MaxLength, MinLength } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; export class AuthCredentialsDto { @@ -17,20 +16,6 @@ export class AuthCredentialsDto { @ApiProperty({ description: '유저 비밀번호', - minLength: 4, - maxLength: 20, - type: 'string', - }) - @IsString() - @MinLength(4) - @MaxLength(20) - email: string; - - @ApiProperty({ - description: '유저 비밀번호', - minLength: 4, - maxLength: 20, - type: 'string', }) @IsString() @MinLength(4) @@ -53,6 +38,4 @@ export class AuthCredentialsDto { @IsString() @IsOptional() kakaoAccessToken?: string; - @Matches(/^[a-zA-Z0-9]*$/) - password: string; } diff --git a/BE/src/auth/strategy/jwt.strategy.ts b/BE/src/auth/strategy/jwt.strategy.ts index a6393996..e6e85d05 100644 --- a/BE/src/auth/strategy/jwt.strategy.ts +++ b/BE/src/auth/strategy/jwt.strategy.ts @@ -2,9 +2,9 @@ import { PassportStrategy } from '@nestjs/passport'; import { InjectRepository } from '@nestjs/typeorm'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { UserRepository } from '../user.repository'; import { User } from '../user.entity'; -import { ConfigService } from '@nestjs/config'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { From 94364bb1a5af7cbea5155f93510a2e6d0f03fff7 Mon Sep 17 00:00:00 2001 From: jinddings Date: Tue, 12 Nov 2024 16:50:40 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20feat=20:=20=EC=B9=B4=EC=B9=B4?= =?UTF-8?q?=EC=98=A4=20login=20=EC=8B=9C=EC=97=90=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=EB=90=98=EC=96=B4=20=EC=9E=88=EC=A7=80=20=EC=95=8A=EC=9C=BC?= =?UTF-8?q?=EB=A9=B4=20=20User=20=EC=A0=95=EB=B3=B4=20=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/auth/auth.controller.ts | 10 ++++++---- BE/src/auth/auth.service.ts | 7 +++++++ BE/src/auth/strategy/kakao.strategy.ts | 6 ++++++ BE/src/auth/user.entity.ts | 6 +++--- BE/src/auth/user.repository.ts | 8 ++++++++ BE/src/stock/order/stock-order.service.ts | 1 - BE/src/types/express.d.ts | 3 ++- 7 files changed, 32 insertions(+), 9 deletions(-) diff --git a/BE/src/auth/auth.controller.ts b/BE/src/auth/auth.controller.ts index d7cdd7c0..98c3d7ad 100644 --- a/BE/src/auth/auth.controller.ts +++ b/BE/src/auth/auth.controller.ts @@ -53,12 +53,14 @@ export class AuthController { @ApiOperation({ summary: 'Kakao 로그인 API' }) @Get('/kakao') @UseGuards(AuthGuard('kakao')) - async kakaoLogin( - @Body() authCredentialsDto: AuthCredentialsDto, - @Res() res: Response, - ) { + async kakaoLogin(@Req() req: Request, @Res() res: Response) { + const authCredentialsDto: AuthCredentialsDto = { + email: req.user.email, + kakaoId: req.user.kakaoId, + }; const { accessToken, refreshToken } = await this.authService.kakaoLoginUser(authCredentialsDto); + res.cookie('accessToken', accessToken, { httpOnly: true }); res.cookie('refreshToken', refreshToken, { httpOnly: true }); res.cookie('isRefreshToken', true, { httpOnly: true }); diff --git a/BE/src/auth/auth.service.ts b/BE/src/auth/auth.service.ts index 2de79cd8..54320e87 100644 --- a/BE/src/auth/auth.service.ts +++ b/BE/src/auth/auth.service.ts @@ -39,6 +39,13 @@ export class AuthService { async kakaoLoginUser( authCredentialsDto: AuthCredentialsDto, ): Promise<{ accessToken: string; refreshToken: string }> { + const user = await this.userRepository.findOne({ + where: { kakaoId: authCredentialsDto.kakaoId }, + }); + + if (!user) { + await this.userRepository.registerKakaoUser(authCredentialsDto); + } return this.getJWTToken(authCredentialsDto); } diff --git a/BE/src/auth/strategy/kakao.strategy.ts b/BE/src/auth/strategy/kakao.strategy.ts index e69805dc..00d6d96f 100644 --- a/BE/src/auth/strategy/kakao.strategy.ts +++ b/BE/src/auth/strategy/kakao.strategy.ts @@ -13,6 +13,9 @@ interface KakaoProfile extends Profile { id: number; _json: { id: number; + kakao_account: { + email: string; + }; }; } @@ -44,7 +47,10 @@ export class KakaoStrategy extends PassportStrategy( try { // eslint-disable-next-line no-underscore-dangle const kakaoId = profile._json.id; + // eslint-disable-next-line no-underscore-dangle + const { email } = profile._json.kakao_account; const user = { + email, kakaoId, }; done(null, user); diff --git a/BE/src/auth/user.entity.ts b/BE/src/auth/user.entity.ts index 8c574cd4..16773177 100644 --- a/BE/src/auth/user.entity.ts +++ b/BE/src/auth/user.entity.ts @@ -15,8 +15,8 @@ export class User extends BaseEntity { @Column({ default: false }) tutorial: boolean; - @Column({ default: -1 }) - kakaoId: number; + @Column({ default: '' }) + kakaoId: string; @Column({ default: '' }) currentRefreshToken: string; @@ -25,7 +25,7 @@ export class User extends BaseEntity { currentRefreshTokenExpiresAt: Date; toAuthCredentialsDto(): AuthCredentialsDto { - if (this.kakaoId === -1) { + if (this.kakaoId === '') { return { email: this.email, password: this.password, diff --git a/BE/src/auth/user.repository.ts b/BE/src/auth/user.repository.ts index 442c73f4..b7a06fa4 100644 --- a/BE/src/auth/user.repository.ts +++ b/BE/src/auth/user.repository.ts @@ -19,6 +19,14 @@ export class UserRepository extends Repository { await this.save(user); } + async registerKakaoUser(authCredentialsDto: AuthCredentialsDto) { + const { kakaoId, email } = authCredentialsDto; + const salt: string = await bcrypt.genSalt(); + const hashedPassword: string = await bcrypt.hash(String(kakaoId), salt); + const user = this.create({ email, kakaoId, password: hashedPassword }); + await this.save(user); + } + async updateUserWithRefreshToken( id: number, { diff --git a/BE/src/stock/order/stock-order.service.ts b/BE/src/stock/order/stock-order.service.ts index 3b5e7e07..997de306 100644 --- a/BE/src/stock/order/stock-order.service.ts +++ b/BE/src/stock/order/stock-order.service.ts @@ -4,7 +4,6 @@ import { Injectable, } from '@nestjs/common'; import { NotFoundError } from 'rxjs'; -import { Injectable } from '@nestjs/common'; import { StockOrderRequestDto } from './dto/stock-order-request.dto'; import { StockOrderRepository } from './stock-order.repository'; import { TradeType } from './enum/trade-type'; diff --git a/BE/src/types/express.d.ts b/BE/src/types/express.d.ts index 9cf89153..2060c079 100644 --- a/BE/src/types/express.d.ts +++ b/BE/src/types/express.d.ts @@ -4,8 +4,9 @@ import { UUID } from 'crypto'; declare module 'express' { interface Request extends Req { user: { - kakaoId?: number; + kakaoId?: string; userId?: UUID; + email?: string; }; } } From 2d1ba002fd238ab4235ea830fb94c068641de84e Mon Sep 17 00:00:00 2001 From: jinddings Date: Tue, 12 Nov 2024 23:43:33 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor=20:=20swagger?= =?UTF-8?q?=20docs=EC=97=90=20=ED=95=84=EC=88=98=EA=B0=80=20=EC=95=84?= =?UTF-8?q?=EB=8B=8C=20=EA=B0=92=20=ED=91=9C=EC=8B=9C=ED=95=98=EA=B2=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/auth/dto/auth-credentials.dto.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BE/src/auth/dto/auth-credentials.dto.ts b/BE/src/auth/dto/auth-credentials.dto.ts index 31bc69b6..90b81254 100644 --- a/BE/src/auth/dto/auth-credentials.dto.ts +++ b/BE/src/auth/dto/auth-credentials.dto.ts @@ -27,6 +27,7 @@ export class AuthCredentialsDto { @ApiProperty({ description: '카카오 ID', + required: false, }) @IsString() @IsOptional() @@ -34,6 +35,7 @@ export class AuthCredentialsDto { @ApiProperty({ description: '카카오 액세스 토큰', + required: false, }) @IsString() @IsOptional()