-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] 2차배포 (11/27)
- Loading branch information
Showing
159 changed files
with
3,917 additions
and
1,663 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ jest.mock('bcrypt', () => ({ | |
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
let userService: UserService; | ||
|
||
let jwtService: JwtService; | ||
|
||
// Mock UserService | ||
|
@@ -49,7 +49,7 @@ describe('AuthService', () => { | |
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
userService = module.get<UserService>(UserService); | ||
|
||
jwtService = module.get<JwtService>(JwtService); | ||
|
||
// Clear all mocks before each test | ||
|
@@ -63,13 +63,6 @@ describe('AuthService', () => { | |
password: 'password123', | ||
}; | ||
|
||
const mockUser = { | ||
id: 1, | ||
username: 'testuser', | ||
password: 'hashedPassword', | ||
email: '[email protected]', | ||
}; | ||
|
||
it('should throw UnauthorizedException if user is not found', async () => { | ||
mockUserService.findUserByUsername.mockResolvedValue(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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { Provider } from '@repo/types'; | ||
|
||
import { CreateSocialUserDto } from '@/user/dto/createSocialUser.dto'; | ||
import { UserService } from '@/user/user.service'; | ||
|
@@ -15,11 +16,7 @@ export class AuthService { | |
) {} | ||
|
||
async signupLocal(signupRequestDto: LocalSignupRequestDto) { | ||
const existingUser = await this.userService.findUserByUsername(signupRequestDto.username); | ||
if (existingUser) { | ||
throw new BadRequestException('이미 사용 중인 사용자 이름입니다.'); | ||
} | ||
return this.userService.createLocalUser({ provider: 'local', ...signupRequestDto }); | ||
return this.userService.createLocalUser({ provider: Provider.local, ...signupRequestDto }); | ||
} | ||
|
||
async validateLocalLogin(username: string, inputPassword: string) { | ||
|
@@ -31,8 +28,27 @@ export class AuthService { | |
if (!isPasswordValid) { | ||
throw new UnauthorizedException('잘못된 로그인 정보'); | ||
} | ||
const { password, ...result } = user; | ||
return result; | ||
return user; | ||
} | ||
|
||
async createGuestUser() { | ||
const randomNum = Math.floor(Math.random() * 10000); | ||
const response = await fetch('https://api.thecatapi.com/v1/images/search'); | ||
const catImageUrl = (await response.json())[0].url; | ||
|
||
const guestUser = { | ||
username: `guest_${randomNum}`, | ||
password: `guest_password_${randomNum}`, | ||
email: `[email protected]`, | ||
nickname: `guest_${randomNum}`, | ||
introduce: `게스트 사용자입니다. `, | ||
profileImageUrl: catImageUrl, | ||
}; | ||
const user = await this.userService.findUserByUsername(guestUser.username); | ||
if (!user) { | ||
return this.userService.createLocalUser({ provider: Provider.guest, ...guestUser }); | ||
} | ||
return user; | ||
} | ||
|
||
async checkSocialUser(socialUserData: CreateSocialUserDto) { | ||
|
@@ -46,7 +62,7 @@ export class AuthService { | |
return user; | ||
} | ||
|
||
async createJWT(userId: number) { | ||
createJWT(userId: number) { | ||
const payload = { sub: userId }; | ||
return { | ||
accessToken: this.jwtService.sign(payload), | ||
|
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 |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ErrorMessage } from '@repo/types'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') { | ||
getRequest(context: ExecutionContext) { | ||
const req = context.switchToHttp().getRequest(); | ||
|
||
const cookies = req.cookies; | ||
|
||
const token = cookies['accessToken']; | ||
|
||
if (token) req.headers.authorization = `Bearer ${token}`; | ||
if (!token) { | ||
throw new UnauthorizedException(ErrorMessage.LOGIN_REQUIRED); | ||
} | ||
req.headers.authorization = `Bearer ${token}`; | ||
|
||
return req; | ||
} | ||
|
||
handleRequest(err: any, user: any) { | ||
if (err || !user) { | ||
throw new UnauthorizedException(ErrorMessage.INVALID_AUTHENTICATION_INFORMATION); | ||
} | ||
return user; | ||
} | ||
} |
Oops, something went wrong.