Skip to content

Commit

Permalink
fix: guest_login bug fix
Browse files Browse the repository at this point in the history
Co-authored-by: 이승관 <[email protected]>
  • Loading branch information
SeongHyeon0409 and SeungGwan123 committed Nov 18, 2024
1 parent 2c785c2 commit eb08da5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 67 deletions.
135 changes: 68 additions & 67 deletions packages/server/src/auth/auth.service.ts
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' };
}
}
}
1 change: 1 addition & 0 deletions packages/server/src/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const jwtConstants = {

export const DEFAULT_KRW = 30000000;
export const DEFAULT_USDT = 300000;
export const DEFAULT_BTC = 10;

0 comments on commit eb08da5

Please sign in to comment.