-
Notifications
You must be signed in to change notification settings - Fork 5
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 #226 from songbuild00/feature-be-#74#77
[BE] feat#74#77 νμκ°μ , λ‘κ·ΈμΈ κ΅¬ν
- Loading branch information
Showing
19 changed files
with
832 additions
and
95 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
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 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
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,26 +1,36 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CreateUserDto } from './dto/create-user.dto'; | ||
import { UpdateUserDto } from './dto/update-user.dto'; | ||
import { Repository } from 'typeorm'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { UserModel } from './entities/user.entity'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { SignupDto } from '../auth/dto/signup.dto'; | ||
|
||
@Injectable() | ||
export class UserService { | ||
create(createUserDto: CreateUserDto) { | ||
return 'This action adds a new user'; | ||
} | ||
|
||
findAll() { | ||
return `This action returns all user`; | ||
} | ||
|
||
findOne(id: number) { | ||
return `This action returns a #${id} user`; | ||
} | ||
constructor( | ||
@InjectRepository(UserModel) | ||
private readonly userRepository: Repository<UserModel> | ||
) {} | ||
|
||
update(id: number, updateUserDto: UpdateUserDto) { | ||
return `This action updates a #${id} user`; | ||
async create(signupDto: SignupDto) { | ||
const salt = await bcrypt.genSalt(); | ||
const hashedPassword = await bcrypt.hash(signupDto.password, salt); | ||
const newUser = this.userRepository.create({ | ||
email: signupDto.email, | ||
password: hashedPassword, | ||
nickname: signupDto.nickname, | ||
status: '?' | ||
}); | ||
return this.userRepository.save(newUser); | ||
} | ||
|
||
remove(id: number) { | ||
return `This action removes a #${id} user`; | ||
async findOne(email: string) { | ||
const user = await this.userRepository.findOne({ | ||
where: { email } | ||
}); | ||
if (!user) { | ||
return null; | ||
} | ||
return user; | ||
} | ||
} |