Skip to content

Commit

Permalink
feat/#65/user, payload 반환 타입 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
gwgw123 committed Jan 8, 2025
1 parent cd56fbd commit f2dcab3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/auth/jwt/jwt.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface TokenPayload {
userId: number;
}

export interface AdminTokenPayload {
role: string;
}
17 changes: 11 additions & 6 deletions src/auth/jwt/jwt.startegy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { UsersService } from 'src/users/services/users.service';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { RedisService } from '../redis/redis.service';
import { User } from 'src/users/entities/user.entity';
import { AdminTokenPayload, TokenPayload } from './jwt.interface';

// accessToken 전략
@Injectable()
Expand Down Expand Up @@ -34,11 +36,9 @@ export class AccessTokenStrategy extends PassportStrategy(
});
}

async validate(request: Request, payload: any): Promise<any> {
console.log('validate 하기 전');
async validate(request: Request, payload: TokenPayload): Promise<User> {
const user = await this.userService.getUser(payload.userId);

console.log('validate 한 후 ');
return user;
}
}
Expand Down Expand Up @@ -71,13 +71,15 @@ export class RefreshTokenStrategy extends PassportStrategy(
});
}

async validate(request: Request, payload: any): Promise<any> {
async validate(request: Request, payload: TokenPayload): Promise<User> {
const refreshToken = request?.cookies?.refreshToken;
if (!refreshToken) {
throw new UnauthorizedException('No Refresh Token provided');
}

const redisRefreshToken = await this.redisService.get(payload.userId);
const redisRefreshToken = await this.redisService.get(
String(payload.userId),
);
if (!redisRefreshToken) {
throw new UnauthorizedException('Refresh Token not found in Redis');
}
Expand Down Expand Up @@ -116,7 +118,10 @@ export class AdminAccessTokenStrategy extends PassportStrategy(
});
}

async validate(request: Request, payload: any): Promise<any> {
async validate(
request: Request,
payload: AdminTokenPayload,
): Promise<object> {
if (payload.role !== 'admin') {
throw new UnauthorizedException('Invalid Access Token');
}
Expand Down
25 changes: 25 additions & 0 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
interface UserModel {
id: number;
provider: string;
providerId: string;
name: string;
level: number;
experience: number;
experienceForNextLevel: number;
point: number;
createdAt: Date;
updatedAt: Date;
}

export class User implements UserModel {
id: number;
provider: string;
providerId: string;
name: string;
level: number;
experience: number;
experienceForNextLevel: number;
point: number;
createdAt: Date;
updatedAt: Date;
}

0 comments on commit f2dcab3

Please sign in to comment.