-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from boostcampwm-2024/feature/api/login-#4
[#4] 2.03 로그인 기능 구현
- Loading branch information
Showing
8 changed files
with
1,379 additions
and
10,776 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,39 @@ | ||
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common'; | ||
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 |
---|---|---|
@@ -1,13 +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])], | ||
imports: [ | ||
TypeOrmModule.forFeature([User]), | ||
PassportModule.register({ defaultStrategy: 'jwt' }), | ||
JwtModule.register({ | ||
secret: 'Juga16', | ||
signOptions: { | ||
expiresIn: 3600, | ||
}, | ||
}), | ||
], | ||
controllers: [AuthController], | ||
providers: [AuthService, UserRepository], | ||
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
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,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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.