Skip to content

Commit

Permalink
fix: 유저를 특정하려면 roomCode 도 jwt에 들어가야 한다
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaemy committed Aug 29, 2023
1 parent 6dc7ca9 commit fdd1576
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/api/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class AuthService {
private readonly prismaService: PrismaService
) {}

createToken(username: string) {
const payload = { username };
createToken(username: string, roomCode: string) {
const payload = { username, roomCode };
return {
accessToken: this.jwtService.sign(payload, {
secret: this.config.jwtSecret,
Expand All @@ -23,9 +23,9 @@ export class AuthService {
};
}

async validateUser(username: string) {
async validateUser(username: string, roomCode: string) {
const user = await this.prismaService.user.findFirst({
where: { username },
where: { username, roomId: roomCode },
include: { room: true },
});

Expand Down
6 changes: 4 additions & 2 deletions src/api/auth/auth.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
}
//토큰 검증
async validate(payload: any) {
const { username } = payload;
const user = await this.authService.validateUser(username);
console.log('validate called');
const { username, roomCode } = payload;
const user = await this.authService.validateUser(username, roomCode);
console.log(user);
return user;
}
}
2 changes: 1 addition & 1 deletion src/api/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class UsersService {
}

const hashedPassword = await hash(password, 10);
const token = this.authService.createToken(username);
const token = this.authService.createToken(username, roomCode);

if (notFoundErrors.length > 0) {
throw new NotFoundException(notFoundErrors);
Expand Down

0 comments on commit fdd1576

Please sign in to comment.