Skip to content

Commit

Permalink
🐛 [Fix]: camperId가 null로 생성되는 이슈 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seungheon123 committed Nov 30, 2024
1 parent ebb86aa commit 94ee16d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 6 deletions.
2 changes: 2 additions & 0 deletions apps/media/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SfuModule } from './sfu/sfu.module';
import { BroadcastModule } from './broadcast/broadcast.module';
import configuration from './config/configuration';
import { AuthModule } from './auth/auth.module';
import { MemberModule } from './member/member.module';

@Module({
imports: [
Expand All @@ -18,6 +19,7 @@ import { AuthModule } from './auth/auth.module';
SfuModule,
BroadcastModule,
AuthModule,
MemberModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
3 changes: 2 additions & 1 deletion apps/media/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { JWTAuthStrategy } from './jwt-auth.strategy';
import { MemberModule } from '../member/member.module';

@Module({
imports: [PassportModule],
imports: [PassportModule, MemberModule],
providers: [JWTAuthStrategy],
})
export class AuthModule {}
2 changes: 1 addition & 1 deletion apps/media/src/auth/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
throw new CustomWsException(ErrorStatus.UNAUTHORIZED);
}
const client = context.switchToWs().getClient();
client.user = user;
client.user = user.data;
return user;
}
}
10 changes: 6 additions & 4 deletions apps/media/src/auth/jwt-auth.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { IPayLoad } from './payload.interface';
import { MemberService } from '../member/member.service';

@Injectable()
export class JWTAuthStrategy extends PassportStrategy(Strategy) {
constructor(private readonly configService: ConfigService) {
constructor(private readonly configService: ConfigService, private readonly memberService: MemberService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.get('JWT_SECRET'),
passReqToCallback: true,
});
}

validate(payLoad: IPayLoad) {
return payLoad;
validate(req: Request) {
const token = req.headers['authorization']?.split(' ')[1]; // Bearer 토큰 형태이므로 'Bearer ' 제거
return this.memberService.findMemberById(token);
}
}
1 change: 1 addition & 0 deletions apps/media/src/auth/payload.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface IPayLoad {
id: number;
camperId: string;
}
1 change: 1 addition & 0 deletions apps/media/src/common/clients/api-client.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface IApiClient {
post<T>(path: string, data: any): Promise<T>;
put<T>(path: string, data?: any): Promise<T>;
delete<T>(path: string, data?: any): Promise<T>;
get<T>(path: string, token: string): Promise<T>;
}
16 changes: 16 additions & 0 deletions apps/media/src/common/clients/api.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IApiClient } from './api-client.interface';
import { ErrorStatus } from '../responses/exceptions/errorStatus';
import { HttpService } from '@nestjs/axios';
import { CustomWsException } from '../responses/exceptions/custom-ws.exception';
import { firstValueFrom } from 'rxjs';

@Injectable()
export class ApiClient implements IApiClient {
Expand Down Expand Up @@ -38,6 +39,21 @@ export class ApiClient implements IApiClient {
}
}

async get<T>(path: string, token?: string): Promise<T> {
try {
const response = await firstValueFrom(
this.httpService.get<T>(`${this.baseUrl}${path}`, {
headers: {
Authorization: `Bearer ${token}`,
},
}),
);
return response.data;
} catch (error) {
throw this.handleError(error);
}
}

// TODO: Error custom 필요
private handleError(_error: any): Error {
throw new CustomWsException(ErrorStatus.API_SERVER_ERROR);
Expand Down
27 changes: 27 additions & 0 deletions apps/media/src/member/member.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Module } from '@nestjs/common';
import { MemberService } from './member.service';
import { ApiClient } from '../common/clients/api.client';
import { HttpModule } from '@nestjs/axios';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
imports: [
HttpModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
timeout: configService.get('HTTP_TIMEOUT'),
maxRedirects: 5,
}),
inject: [ConfigService],
}),
],
providers: [
MemberService,
{
provide: 'API_CLIENT',
useClass: ApiClient,
},
],
exports: [MemberService],
})
export class MemberModule {}
14 changes: 14 additions & 0 deletions apps/media/src/member/member.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Inject, Injectable } from '@nestjs/common';
import { IApiClient } from '../common/clients/api-client.interface';

@Injectable()
export class MemberService {
constructor(
@Inject('API_CLIENT')
private readonly apiClient: IApiClient,
) {}

findMemberById(token: string) {
return this.apiClient.get(`/v1/members/info`, token);
}
}

0 comments on commit 94ee16d

Please sign in to comment.