-
Notifications
You must be signed in to change notification settings - Fork 1
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
[#4] 2.03 로그인 기능 구현 #33
Merged
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
959e10d
✨ feat: module, controller, service, entity, repository 초기 설정 및 의존성 주…
jinddings 8fb0b1f
✨ feat: 회원 가입 레포지토리 구현
jinddings d169098
✨ feat: userRepository registerUser 메소드 구현 (#26)
jinddings 643ad3c
Merge branch 'back/main' of https://github.com/boostcampwm-2024/web16…
jinddings a71bf2c
✨ feat: AuthService 회원가입 로직 구현(#26)
jinddings 92c7549
⚙️ chore: AuthModule을 AppModule에 추가(#26)
jinddings bf295a6
✨ feat: AuthController 구현 및 AuthService에서 비밀번호 암호화 로직 추가(#26)
jinddings 08312cd
⚙️ chore: merge conflict 해결
sieunie 4390c3c
✨ feat: stock websocket gateway 구현 #6
sieunie 3a777d6
✨ feat: 주가 그래프 웹소켓 구현 #6
sieunie ad2b3e6
✨ feat: 주가 지수 값 웹소켓 구현 #6
sieunie 5ef98b0
✨ feat: 주가 지수 그래프 및 값 API 구현 #6
sieunie 2796724
📝 docs: 주가 지수 API 명세 추가
sieunie bad5bb0
⚙️ chore: ws 라이브러리 설치
sieunie c9a16a1
♻️ refactor: cron 로직 service 단으로 이동
sieunie 7117aac
♻️ refactor: dto 파일 분리
sieunie c219c6d
✨ feat: 주가 지수 소켓 통신으로 변경
sieunie 335686b
♻️ refactor: return type dto로 변경
sieunie 6aafebe
♻️ refactor: 주가 정보 관련 interface 생성 및 적용 #6
sieunie 226ce86
✨ feat: 가입 요청 유저 정보에 대한 유효성 검사 추가 (#26)
jinddings 60c03e1
🔧 fix: 타입 버그 수정
sieunie a86bf21
✨ feat: access_token을 발급받는 로직 구현#12
uuuo3o 414b03c
✨ feat: 오늘의 상/하위 종목 조회에 사용할 DTO 구현#12
uuuo3o 2af0a7d
✨ feat: 한국투자 API 요청 및 결과값을 받아오는 로직 구현#12
uuuo3o f8c7046
✨ feat: 오늘의 상/하위 종목 리스트 API Controller 구현#12
uuuo3o ba8e2d6
⚙️ chore: TopFiveModule을 AppModule에 추가#12
uuuo3o eee8101
⚙️ chore:lint 실행시 발생하는 오류 수정
jinddings 4b1ed0e
Merge branch 'back/main' of https://github.com/boostcampwm-2024/web16…
jinddings 7736420
✨ feat: 상/하위 종목 5개만 반환할 수 있도록 수정#12
uuuo3o 5003ca6
✨ feat: UserRepository loginUser 메소드 구현(#4)
jinddings e2a14e8
➕ add: module layer 추가 #6
sieunie 93141c4
➕ add: 코스피200, KSQ150 지수 정보 추가 #6
sieunie 9e72aff
⚙️ chore: 린트 오류 해결
sieunie 8b956ed
✨ feat: JWT 토큰을 이용한 로그인 기능 구현(#4)
jinddings 3c1ffcb
💡 comment: Console.error 주석 처리#12
uuuo3o e4ba28a
✨ feat: AuthGuard 를 이용한 토큰 인증 기능 구현(#4)
jinddings 8b121d1
⚙️ chore: lint,test 에러 발생 수정
jinddings 4375e5f
🔧 fix: 주석처리 했던 코드를 logger로 변경#12
uuuo3o 9887ea6
� docs: 로그인 API 명세 추가
jinddings 83d246a
Merge pull request #30 from boostcampwm-2024/feature/api/topfive-#12
uuuo3o cc860a5
Merge branch 'back/main' into feature/api/stockindex-#6
sieunie a190aa7
Merge pull request #32 from boostcampwm-2024/feature/api/stockindex-#6
sieunie e817f4b
Merge branch 'back/main' into feature/api/register-#26
jinddings e9f5950
Merge pull request #29 from boostcampwm-2024/feature/api/register-#26
jinddings 81cc0ef
⚙️ chore: merge 오류 해결
sieunie abbc19b
⚙️ chore: merge 오류 해결
sieunie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { UserRepository } from './user.repository'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [ | ||
AuthService, | ||
{ | ||
provide: UserRepository, | ||
useValue: { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
Controller, | ||
Post, | ||
Get, | ||
Body, | ||
Req, | ||
ValidationPipe, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiOperation } from '@nestjs/swagger'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthCredentialsDto } from './dto/authCredentials.dto'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@ApiOperation({ summary: '회원 가입 API' }) | ||
@Post('/signup') | ||
signUp(@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto) { | ||
return this.authService.signUp(authCredentialsDto); | ||
} | ||
|
||
@ApiOperation({ summary: '로그인 API' }) | ||
@Get('/login') | ||
loginWithCredentials( | ||
@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto, | ||
) { | ||
return this.authService.loginUser(authCredentialsDto); | ||
} | ||
|
||
@ApiOperation({ summary: 'Token 인증 테스트 API' }) | ||
@Get('/test') | ||
@UseGuards(AuthGuard()) | ||
test(@Req() req: Request) { | ||
return req; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { User } from './user.entity'; | ||
import { UserRepository } from './user.repository'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([User]), | ||
PassportModule.register({ defaultStrategy: 'jwt' }), | ||
JwtModule.register({ | ||
secret: 'Juga16', | ||
signOptions: { | ||
expiresIn: 3600, | ||
}, | ||
}), | ||
], | ||
controllers: [AuthController], | ||
providers: [AuthService, UserRepository, JwtStrategy], | ||
exports: [JwtStrategy, PassportModule], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthController } from './auth.controller'; | ||
import { UserRepository } from './user.repository'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [ | ||
AuthService, | ||
{ | ||
provide: UserRepository, | ||
useValue: { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { UserRepository } from './user.repository'; | ||
import { AuthCredentialsDto } from './dto/authCredentials.dto'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
@InjectRepository(UserRepository) | ||
private userRepository: UserRepository, | ||
private jwtService: JwtService, | ||
) {} | ||
|
||
async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> { | ||
return this.userRepository.registerUser(authCredentialsDto); | ||
} | ||
|
||
async loginUser( | ||
authCredentialsDto: AuthCredentialsDto, | ||
): Promise<{ accessToken: string }> { | ||
const { email, password } = authCredentialsDto; | ||
const user = await this.userRepository.findOne({ where: { email } }); | ||
|
||
if (user && (await bcrypt.compare(password, user.password))) { | ||
const payload = { email }; | ||
const accessToken = this.jwtService.sign(payload); | ||
return { accessToken }; | ||
} | ||
throw new UnauthorizedException('Please check your login credentials'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { IsString, Matches, MaxLength, MinLength } from 'class-validator'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
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) | ||
@MaxLength(20) | ||
@Matches(/^[a-zA-Z0-9]*$/) | ||
password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { UserRepository } from './user.repository'; | ||
import { User } from './user.entity'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor( | ||
@InjectRepository(UserRepository) private userRepository: UserRepository, | ||
) { | ||
super({ | ||
secretOrKey: 'Juga16', | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
}); | ||
} | ||
|
||
async validate(payload) { | ||
const { email } = payload; | ||
const user: User = await this.userRepository.findOne({ where: { email } }); | ||
if (!user) throw new UnauthorizedException(); | ||
|
||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity() | ||
export class User extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
email: string; | ||
|
||
@Column() | ||
password: string; | ||
|
||
@Column({ default: false }) | ||
tutorial: boolean; | ||
|
||
@Column({ default: -1 }) | ||
kakaoId: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectDataSource } from '@nestjs/typeorm'; | ||
import { DataSource, Repository } from 'typeorm'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { User } from './user.entity'; | ||
import { AuthCredentialsDto } from './dto/authCredentials.dto'; | ||
|
||
@Injectable() | ||
export class UserRepository extends Repository<User> { | ||
constructor(@InjectDataSource() dataSource: DataSource) { | ||
super(User, dataSource.createEntityManager()); | ||
} | ||
|
||
async registerUser(authCredentialsDto: AuthCredentialsDto) { | ||
const { email, password } = authCredentialsDto; | ||
const salt: string = await bcrypt.genSalt(); | ||
const hashedPassword: string = await bcrypt.hash(password, salt); | ||
const user = this.create({ email, password: hashedPassword }); | ||
await this.save(user); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
BE/src/stock/index/dto/stock.index.list.chart.element.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class StockIndexListChartElementDto { | ||
constructor(time: string, value: string) { | ||
this.time = time; | ||
this.value = value; | ||
} | ||
|
||
@ApiProperty({ description: 'HHMMSS', example: '130500' }) | ||
time: string; | ||
|
||
@ApiProperty({ description: '주가 지수' }) | ||
value: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { StockIndexListChartElementDto } from './stock.index.list.chart.element.dto'; | ||
|
||
export class StockIndexListElementDto { | ||
constructor(code: string, chart: StockIndexListChartElementDto[]) { | ||
this.code = code; | ||
this.chart = chart; | ||
} | ||
|
||
@ApiProperty({ | ||
description: '코스피: 0001, 코스닥: 1001, 코스피200: 2001, KSQ150: 3003', | ||
}) | ||
code: string; | ||
|
||
@ApiProperty({ type: [StockIndexListChartElementDto] }) | ||
chart: StockIndexListChartElementDto[]; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🔴 얘가 혹시 JWT secret key인가요???? 이거는 노출되면 안될 거 같은데..ㅎㅎㅎㅎ
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.
수정하겠습니다!