Skip to content

Commit 9b2134a

Browse files
authored
Merge pull request #239 from boostcampwm-2024/feat/#236-add-guest-mode
Feat/#236 add guest mode
2 parents 07dad69 + a365362 commit 9b2134a

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

back/src/domains/user/controller/user.controller.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ export class UserController {
7979
return { message: '관리자 회원가입이 성공적으로 완료되었습니다.' };
8080
}
8181

82+
@ApiOperation({ summary: '게스트 모드', description: '게스트 모드 요청을 받아 게스트 계정을 생성해준다.' })
83+
@ApiOkResponse({
84+
description: '게스트 모드 성공',
85+
example: {
86+
id: 10,
87+
loginId: 'guest-a204cf2e-4243-4998-bb6a-4649b040f86f',
88+
userStatus: 'LOGIN',
89+
targetEvent: null,
90+
},
91+
})
92+
@ApiInternalServerErrorResponse({ description: '게스트를 생성하는데 실패하였습니다.' })
93+
@Get('/guest')
94+
async useGuestMode(@Res({ passthrough: true }) res: Response) {
95+
const { sessionId, userInfo } = await this.userService.makeGuestUser();
96+
res.cookie('SID', sessionId, { httpOnly: true });
97+
98+
return userInfo;
99+
}
100+
82101
@ApiOperation({ summary: '로그인', description: 'id, password를 받아 로그인 요청을 처리한다.' })
83102
@ApiBody({
84103
type: UserLoginDto,

back/src/domains/user/entity/user.entity.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export class User {
1616
@Column({ type: 'varchar', length: 10, name: 'role' })
1717
role: string;
1818

19+
@Column({ type: 'boolean', name: 'is_guest' })
20+
checkGuest: boolean;
21+
1922
@OneToMany(() => Reservation, (reservation) => reservation.user, { lazy: true })
2023
reservations: Promise<Reservation[]>;
2124
}

back/src/domains/user/service/user.service.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@nestjs/common';
1010
import * as bcrypt from 'bcryptjs';
1111
import Redis from 'ioredis';
12+
import { DataSource } from 'typeorm';
1213
import { v4 as uuidv4 } from 'uuid';
1314

1415
import { USER_STATUS } from '../../../auth/const/userStatus.const';
@@ -30,6 +31,7 @@ export class UserService {
3031
@Inject() private readonly userRepository: UserRepository,
3132
@Inject() private readonly redisService: RedisService,
3233
@Inject() private readonly authService: AuthService,
34+
@Inject() private readonly dataSource: DataSource,
3335
) {
3436
this.redis = this.redisService.getOrThrow();
3537
}
@@ -148,4 +150,33 @@ export class UserService {
148150
const session = JSON.parse(await this.redis.get(`user:${sid}`));
149151
return session.targetEvent;
150152
}
153+
154+
async makeGuestUser() {
155+
try {
156+
// make guest user
157+
const uuid = uuidv4();
158+
const guestId = `guest-${uuid}`;
159+
160+
const guestInfo = await this.userRepository.createUser({
161+
loginId: guestId,
162+
role: USER_ROLE.USER,
163+
checkGuest: true,
164+
});
165+
166+
const guestSession = {
167+
id: guestInfo.id,
168+
loginId: guestInfo.loginId,
169+
userStatus: USER_STATUS.LOGIN,
170+
targetEvent: null,
171+
};
172+
173+
this.redis.set(`user-id:${guestId}`, uuid, 'EX', 3600);
174+
this.redis.set(`user:${uuid}`, JSON.stringify(guestSession), 'EX', 3600);
175+
176+
return { sessionId: uuid, userInfo: guestSession };
177+
} catch (err) {
178+
this.logger.error(err.name, err.stack);
179+
throw new InternalServerErrorException('게스트 사용자 생성에 실패하였습니다.');
180+
}
181+
}
151182
}

0 commit comments

Comments
 (0)