-
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.
Showing
2 changed files
with
69 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,90 @@ | ||
import { ConflictException, Injectable } from '@nestjs/common'; | ||
import { UserRepository } from './user.repository'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { DEFAULT_KRW, DEFAULT_USDT, jwtConstants } from './constants'; | ||
import { DEFAULT_BTC, DEFAULT_KRW, DEFAULT_USDT, jwtConstants } from './constants'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { AccountRepository } from 'src/account/account.repository'; | ||
import { RedisRepository } from 'src/redis/redis.repository'; | ||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private userRepository: UserRepository, | ||
private accountRepository: AccountRepository, | ||
private jwtService: JwtService, | ||
private readonly redisRepository: RedisRepository, | ||
) {} | ||
constructor( | ||
private userRepository: UserRepository, | ||
private accountRepository: AccountRepository, | ||
private jwtService: JwtService, | ||
private readonly redisRepository: RedisRepository, | ||
) {} | ||
|
||
async signIn(username: string): Promise<{ access_token: string }> { | ||
const user = await this.userRepository.findOneBy({ username }); | ||
const payload = { userId: user.id, userName: user.username }; | ||
return { | ||
access_token: await this.jwtService.signAsync(payload, { | ||
secret: jwtConstants.secret, | ||
expiresIn: '6000s', | ||
}), | ||
}; | ||
} | ||
async signIn(username: string): Promise<{ access_token: string }> { | ||
const user = await this.userRepository.findOneBy({ username }); | ||
const payload = { userId: user.id, userName: user.username }; | ||
return { | ||
access_token: await this.jwtService.signAsync(payload, { | ||
secret: jwtConstants.secret, | ||
expiresIn: '6000s', | ||
}), | ||
}; | ||
} | ||
|
||
async guestSignIn(): Promise<{ access_token: string }> { | ||
const username = `guest_${uuidv4()}`; | ||
async guestSignIn(): Promise<{ access_token: string }> { | ||
const username = `guest_${uuidv4()}`; | ||
|
||
await this.signUp(username, true); | ||
await this.signUp(username, true); | ||
|
||
const guestUser = await this.userRepository.findOneBy({ username }); | ||
const guestUser = await this.userRepository.findOneBy({ username }); | ||
|
||
await this.redisRepository.setAuthData( | ||
`guest:${guestUser.id}`, | ||
JSON.stringify({ userId: guestUser.id }), | ||
6000, | ||
); | ||
await this.redisRepository.setAuthData( | ||
`guest:${guestUser.id}`, | ||
JSON.stringify({ userId: guestUser.id }), | ||
6000, | ||
); | ||
|
||
const payload = { userId: guestUser.id, userName: guestUser.username }; | ||
return { | ||
access_token: await this.jwtService.signAsync(payload, { | ||
secret: jwtConstants.secret, | ||
expiresIn: '6000s', | ||
}), | ||
}; | ||
} | ||
const payload = { userId: guestUser.id, userName: guestUser.username }; | ||
return { | ||
access_token: await this.jwtService.signAsync(payload, { | ||
secret: jwtConstants.secret, | ||
expiresIn: '6000s', | ||
}), | ||
}; | ||
} | ||
|
||
async signUp( | ||
username: string, | ||
isGuest = false, | ||
): Promise<{ message: string }> { | ||
const existingUser = await this.userRepository.findOneBy({ username }); | ||
if (existingUser) { | ||
throw new ConflictException('Username already exists'); | ||
} | ||
async signUp( | ||
username: string, | ||
isGuest = false, | ||
): Promise<{ message: string }> { | ||
const existingUser = await this.userRepository.findOneBy({ username }); | ||
if (existingUser) { | ||
throw new ConflictException('Username already exists'); | ||
} | ||
|
||
const newUser = await this.userRepository.save({ | ||
username, | ||
isGuest, | ||
}); | ||
const newUser = await this.userRepository.save({ | ||
username, | ||
isGuest, | ||
}); | ||
|
||
await this.accountRepository.save({ | ||
user: newUser, | ||
KRW: DEFAULT_KRW, | ||
USDT: DEFAULT_USDT, | ||
}); | ||
await this.accountRepository.save({ | ||
user: newUser, | ||
KRW: DEFAULT_KRW, | ||
USDT: DEFAULT_USDT, | ||
BTC: DEFAULT_BTC | ||
}); | ||
|
||
return { | ||
message: isGuest | ||
? 'Guest user successfully registered' | ||
: 'User successfully registered', | ||
}; | ||
} | ||
return { | ||
message: isGuest | ||
? 'Guest user successfully registered' | ||
: 'User successfully registered', | ||
}; | ||
} | ||
|
||
async logout(userId: number): Promise<{ message: string }> { | ||
const user = await this.userRepository.findOneBy({ id: userId }); | ||
async logout(userId: number): Promise<{ message: string }> { | ||
const user = await this.userRepository.findOneBy({ id: userId }); | ||
|
||
if (!user) { | ||
throw new Error('User not found'); | ||
} | ||
if (!user) { | ||
throw new Error('User not found'); | ||
} | ||
|
||
if (user.isGuest) { | ||
await this.userRepository.delete({ id: userId }); | ||
return { message: 'Guest user data successfully deleted' }; | ||
} | ||
} | ||
if (user.isGuest) { | ||
await this.userRepository.delete({ id: userId }); | ||
return { message: 'Guest user data successfully deleted' }; | ||
} | ||
} | ||
} |
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