-
Notifications
You must be signed in to change notification settings - Fork 5
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
[BE] feat#74#77 회원가입, 로그인 구현 #226
Merged
songbuild00
merged 3 commits into
boostcampwm-2024:dev-be
from
songbuild00:feature-be-#74#77
Nov 20, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController] | ||
}).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,27 @@ | ||
import { Body, Controller, Get, Post, Request, UseGuards } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { LocalAuthGuard } from './guard/local-auth.guard'; | ||
import { JwtAuthGuard } from './guard/jwt-auth.guard'; | ||
import { SignupDto } from './dto/signup.dto'; | ||
|
||
@Controller('/api/auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@Post('signup') | ||
async signup(@Body() signupDto: SignupDto) { | ||
return this.authService.signup(signupDto); | ||
} | ||
|
||
@UseGuards(LocalAuthGuard) | ||
@Post('login') | ||
async login(@Request() req) { | ||
return this.authService.login(req.user); | ||
} | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Get('profile') | ||
async profile(@Request() req) { | ||
return req.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,26 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthController } from './auth.controller'; | ||
import { UserModule } from '../user/user.module'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { JwtStrategy } from './guard/jwt.strategy'; | ||
import { LocalStrategy } from './guard/local.strategy'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
|
||
@Module({ | ||
imports: [ | ||
UserModule, | ||
PassportModule, | ||
JwtModule.registerAsync({ | ||
imports: [ConfigModule], | ||
useFactory: () => ({ | ||
secret: process.env.JWT_SECRET || 'SECRET_KEY', | ||
signOptions: { expiresIn: '1d' } | ||
}) | ||
}) | ||
], | ||
providers: [AuthService, LocalStrategy, JwtStrategy], | ||
controllers: [AuthController] | ||
}) | ||
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}).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,45 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { UserService } from '../user/user.service'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { UserModel } from '../user/entities/user.entity'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { SignupDto } from './dto/signup.dto'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private userService: UserService, | ||
private jwtService: JwtService | ||
) {} | ||
|
||
async validateUserInLocal(email: string, password: string) { | ||
const user = await this.userService.findOne(email); | ||
if (user && (await bcrypt.compare(password, user.password))) { | ||
return user; | ||
} | ||
return null; | ||
} | ||
|
||
async validateUserInJwt(id: number, email: string) { | ||
const user = await this.userService.findOne(email); | ||
if (user && user.id === id) { | ||
return user; | ||
} | ||
return null; | ||
} | ||
|
||
async login(user: UserModel) { | ||
const payload = { sub: user.id, email: user.email }; | ||
return { | ||
access_token: this.jwtService.sign(payload) | ||
}; | ||
} | ||
|
||
async signup(signupDto: SignupDto) { | ||
const user = await this.userService.create(signupDto); | ||
if (user) { | ||
return user; | ||
} | ||
return null; | ||
} | ||
} |
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,12 @@ | ||
import { IsEmail, IsString } from 'class-validator'; | ||
|
||
export class SignupDto { | ||
@IsEmail() | ||
email: string; | ||
|
||
@IsString() | ||
password: string; | ||
|
||
@IsString() | ||
nickname: 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,5 @@ | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} |
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,24 @@ | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthService } from '../auth.service'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor(private authService: AuthService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: process.env.JWT_SECRET || 'SECRET_KEY' | ||
}); | ||
} | ||
|
||
async validate(payload: any) { | ||
console.log(`TEST VALIDATE ${payload}`); | ||
const user = await this.authService.validateUserInJwt(payload.sub, payload.username); | ||
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,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class LocalAuthGuard extends AuthGuard('local') {} |
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 { Strategy } from 'passport-local'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthService } from '../auth.service'; | ||
|
||
@Injectable() | ||
export class LocalStrategy extends PassportStrategy(Strategy) { | ||
constructor(private authService: AuthService) { | ||
super({ usernameField: 'email', passwordField: 'password' }); | ||
} | ||
|
||
async validate(email: string, password: string): Promise<any> { | ||
const user = await this.authService.validateUserInLocal(email, password); | ||
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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.
로컬 가드는 역할이 뭔가요?
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.
로그인할때 body로 email, pw 얻어와서 jwt로 바꾸는역할