Skip to content

Commit

Permalink
feat: 게스트 로그인 클라측에서 지정받은 id를 기반으로 생성할수 있게 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
mssak committed Nov 28, 2024
1 parent 715c8db commit be276ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
9 changes: 6 additions & 3 deletions packages/backend/gameserver/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { AuthService } from './auth.service';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Get('guest-login')
guestLogin(): { userId: string; password: string } {
return this.authService.guestLogin();
guestLogin(@Query('userId') userId: string): {
userId: string;
password: string;
} {
return this.authService.guestLogin(userId || '게스트');
}
}
20 changes: 11 additions & 9 deletions packages/backend/gameserver/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Injectable } from '@nestjs/common';
import { v4 as uuidv4 } from 'uuid';

interface GuestCredentials {
userId: string;
password: string;
}

@Injectable()
export class AuthService {
private guestSessions: Map<string, string> = new Map();
private guestCounter: number = 1;
private userCounters: Map<string, number> = new Map();

guestLogin(): GuestCredentials {
const userId = `게스트_${this.guestCounter++}`;
const password = uuidv4();
guestLogin(requestedUserId: string): { userId: string; password: string } {
let userId = requestedUserId;

// 이미 존재하는 userId인 경우 카운터를 증가시켜 새로운 userId 생성
if (this.guestSessions.has(userId)) {
const counter = (this.userCounters.get(requestedUserId) || 1) + 1;
this.userCounters.set(requestedUserId, counter);
userId = `${requestedUserId}_${counter}`;
}

const password = uuidv4();
this.guestSessions.set(userId, password);
return { userId, password };
}
Expand Down

0 comments on commit be276ff

Please sign in to comment.