Skip to content

Commit

Permalink
fix: managing modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tomatozil committed Feb 20, 2024
1 parent 8106f9d commit 6372cd8
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 156 deletions.
4 changes: 1 addition & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { AuthGuard } from '@nestjs/passport';
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { User } from 'src/users/entities/user.entity';
import { AppService } from '../app.service';
import { SignupRequestDto } from '../users/dto/signup-request.dto';
import { UsersService } from '../users/users.service';
import { AuthService } from './auth.service';
Expand All @@ -27,7 +26,6 @@ export class AuthController {
private readonly authService: AuthService,
private readonly ftAuthService: FtAuthService,
private readonly usersService: UsersService,
private readonly AppService: AppService,
) {}

@Post('/signin')
Expand All @@ -51,7 +49,7 @@ export class AuthController {
const jwtAccessToken = await this.authService.generateJwtToken(user);

// MFA가 활성화 되어있지 않다면 jwt 토큰을 쿠키에 저장한다.
if (user.isMfaEnabled == false) {
if (!user.isMfaEnabled) {
// token을 쿠키에 저장한다.
res.cookie('accessToken', jwtAccessToken, {
// httpOnly: true, // 자동로그인을 위해 httpOnly를 false로 설정
Expand Down
37 changes: 3 additions & 34 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,31 @@ import { Module } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppService } from 'src/app.service';
import { Game } from 'src/game/entities/game.entity';
import { GameRepository } from 'src/game/game.repository';
import { BlocksRepository } from 'src/users/blocks.repository';
import { Block } from 'src/users/entities/block.entity';
import { Friend } from 'src/users/entities/friend.entity';
import { FriendsRepository } from 'src/users/friends.repository';
import { ChannelUsersRepository } from '../channels/channel-users.repository';
import { ChannelsGateway } from '../channels/channels.gateway';
import { ChannelUser } from '../channels/entities/channel-user.entity';
import jwtConfig from '../config/jwt.config';
import { GameInvitation } from '../game/entities/game-invitation.entity';
import { GameInvitationRepository } from '../game/game-invitation.repository';
import { User } from '../users/entities/user.entity';
import { UsersRepository } from '../users/users.repository';
import { UsersService } from '../users/users.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { FtAuthService } from './ft-auth.service';
import { JwtAccessStrategy } from './jwt-access.strategy';
import { JwtRefreshStrategy } from './jwt-refresh.strategy';
import { UsersModule } from '../users/users.module';

@Module({
imports: [
TypeOrmModule.forFeature([
User,
Game,
GameInvitation,
Friend,
Block,
ChannelUser,
]),
PassportModule,
JwtModule.registerAsync({
inject: [jwtConfig.KEY],
useFactory: (jwtConfigure: ConfigType<typeof jwtConfig>) =>
jwtConfigure,
}),
UsersModule,
],
controllers: [AuthController],
providers: [
AuthService,
FtAuthService,
UsersRepository,
UsersService,
GameRepository,
GameInvitationRepository,
FriendsRepository,
BlocksRepository,
ChannelUsersRepository,
FriendsRepository,
AppService,
JwtAccessStrategy,
JwtRefreshStrategy,
],
exports: [JwtAccessStrategy, JwtRefreshStrategy],
exports: [AuthService, JwtAccessStrategy, JwtRefreshStrategy],
})
export class AuthModule {}
29 changes: 0 additions & 29 deletions src/auth/guards/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,3 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
return super.canActivate(context);
}
}

// @Injectable()
// export class JwtAuthGuard extends PassportStrategy(Strategy, 'access') {
// constructor(
// @InjectRepository(UsersRepository)
// private readonly userRepository: UsersRepository,
// @Inject(jwtConfig.KEY)
// private readonly jwtConfigure: ConfigType<typeof jwtConfig>,
// ) {
// super({
// secretOrKey: jwtConfigure.secret,
// jwtFromRequest: ExtractJwt.fromExtractors([
// (request) => request.cookies?.accessToken,
// ]),
// // ignoreExpiration: true,
// });
// }

// async validate({ id }: JwtAccessPayloadDto): Promise<User> {
// console.log('jwt-auth.guard.ts: validate: id: ', id);
// const user = await this.userRepository.findOneBy({ id });

// if (!user) {
// throw UnauthorizedException();
// }

// return user;
// }
// }
4 changes: 2 additions & 2 deletions src/channels/channels.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class ChannelsGateway
async handleDisconnect(@ConnectedSocket() client: SocketWithAuth) {
this.logger.log(`Client disconnected: ${client.id}`);
const gameQueue = this.gameQueue;
const user = await this.authService.getUserFromSocket(client);
const user = client.user;
if (!user || client.id !== user.channelSocketId) {
return;
}
Expand Down Expand Up @@ -162,7 +162,7 @@ export class ChannelsGateway
@MessageBody() data: EventMessageOnDto,
) {
// Socket으로부터 user 정보를 가져온다.
const user = await this.authService.getUserFromSocket(client);
const user = client.user;

if (!user || user.channelSocketId !== client.id) {
throw WSBadRequestException('유저 정보가 일치하지 않습니다.'); // TODO: exception 발생해도 서버 죽지 않는지 확인
Expand Down
29 changes: 5 additions & 24 deletions src/channels/channels.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { Module } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthService } from 'src/auth/auth.service';
import { Game } from 'src/game/entities/game.entity';
import { GameRepository } from 'src/game/game.repository';
import { BlocksRepository } from 'src/users/blocks.repository';
import { Block } from 'src/users/entities/block.entity';
import { Friend } from 'src/users/entities/friend.entity';
import { User } from 'src/users/entities/user.entity';
import { FriendsRepository } from 'src/users/friends.repository';
import { UsersRepository } from 'src/users/users.repository';
import { UsersService } from 'src/users/users.service';
import { GameInvitation } from '../game/entities/game-invitation.entity';
import { GameInvitationRepository } from '../game/game-invitation.repository';
import { ChannelInvitationRepository } from './channel-invitation.repository';
import { ChannelUsersRepository } from './channel-users.repository';
import { ChannelsController } from './channels.controller';
Expand All @@ -22,35 +15,23 @@ import { ChannelsService } from './channels.service';
import { ChannelInvitation } from './entities/channel-invitation.entity';
import { ChannelUser } from './entities/channel-user.entity';
import { Channel } from './entities/channel.entity';
import { UsersModule } from '../users/users.module';
import { AuthModule } from '../auth/auth.module';

@Module({
imports: [
TypeOrmModule.forFeature([
Channel,
ChannelUser,
ChannelInvitation,
User,
Game,
GameInvitation,
Friend,
Block,
]),
TypeOrmModule.forFeature([Channel, ChannelUser, ChannelInvitation]),
AuthModule,
UsersModule,
],
controllers: [ChannelsController],
providers: [
ChannelsService,
UsersService,
AuthService,
JwtService,
ChannelsRepository,
ChannelUsersRepository,
ChannelInvitationRepository,
UsersRepository,
ChannelsGateway,
GameRepository,
GameInvitationRepository,
FriendsRepository,
BlocksRepository,
],
exports: [ChannelsGateway],
})
Expand Down
2 changes: 1 addition & 1 deletion src/common/exception/custom-global-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class GlobalExceptionFilter implements ExceptionFilter {
errorCode = exception.getState() ? exception.getState() : 500;
message = exception.message
? exception.message
: 'Internal Server Error';
: 'Unexpected Server Error';
}

response.json({
Expand Down
30 changes: 0 additions & 30 deletions src/config/multer.config.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect {
private gameIdToGameDto: Map<number, GameDto>;

constructor(
private readonly authService: AuthService,
private readonly usersRepository: UsersRepository,
private readonly friendsRepository: FriendsRepository,
private readonly gameRepository: GameRepository,
Expand Down
29 changes: 5 additions & 24 deletions src/game/game.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,27 @@ import { Game } from './entities/game.entity';
import { GameController } from './game.controller';
import { GameRepository } from './game.repository';
import { GameService } from './game.service';
import { UsersRepository } from '../users/users.repository';
import { User } from '../users/entities/user.entity';
import { GameInvitationRepository } from './game-invitation.repository';
import { GameInvitation } from './entities/game-invitation.entity';
import { GameGateway } from './game.gateway';
import { AuthService } from '../auth/auth.service';
import { JwtService } from '@nestjs/jwt';
import { ChannelUsersRepository } from '../channels/channel-users.repository';
import { FriendsRepository } from '../users/friends.repository';
import { ChannelUser } from '../channels/entities/channel-user.entity';
import { Friend } from '../users/entities/friend.entity';
import { ChannelsModule } from '../channels/channels.module';
import { Block } from '../users/entities/block.entity';
import { BlocksRepository } from '../users/blocks.repository';
import { ChannelsGateway } from 'src/channels/channels.gateway';
import { UsersModule } from '../users/users.module';

@Module({
imports: [
TypeOrmModule.forFeature([
Game,
GameInvitation,
User,
ChannelUser,
Friend,
Block,
]),
TypeOrmModule.forFeature([Game, GameInvitation]),
UsersModule,
ChannelsModule,
],
controllers: [GameController],
providers: [
JwtService,
GameGateway,
GameService,
AuthService,
JwtService,
ChannelUsersRepository,
FriendsRepository,
BlocksRepository,
GameRepository,
GameInvitationRepository,
UsersRepository,
],
exports: [GameRepository],
})
export class GameModule {}
19 changes: 11 additions & 8 deletions src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Game } from 'src/game/entities/game.entity';
import { GameRepository } from 'src/game/game.repository';
import { BlocksRepository } from './blocks.repository';
import { BlocksService } from './blocks.service';
import { Block } from './entities/block.entity';
Expand All @@ -15,13 +13,15 @@ import { UsersRepository } from './users.repository';
import { UsersService } from './users.service';
import { ScheduleModule } from '@nestjs/schedule';
import { AppService } from 'src/app.service';
import { GameInvitation } from '../game/entities/game-invitation.entity';
import { GameInvitationRepository } from '../game/game-invitation.repository';
import {GameModule} from "../game/game.module";
import {Game} from "../game/entities/game.entity";
import {GameRepository} from "../game/game.repository";

@Module({
imports: [
TypeOrmModule.forFeature([User, Friend, Block, Game, GameInvitation]),
TypeOrmModule.forFeature([User, Friend, Block]),
ScheduleModule.forRoot(),
GameModule,
],
controllers: [UsersController],
providers: [
Expand All @@ -32,10 +32,13 @@ import { GameInvitationRepository } from '../game/game-invitation.repository';
UsersRepository,
FriendsRepository,
BlocksRepository,
GameRepository,
GameInvitationRepository,
AppService,
],
exports: [UsersService],
exports: [
UsersService,
UsersRepository,
FriendsRepository,
BlocksRepository,
],
})
export class UsersModule {}

0 comments on commit 6372cd8

Please sign in to comment.