diff --git a/src/app.controller.ts b/src/app.controller.ts new file mode 100644 index 0000000..e748170 --- /dev/null +++ b/src/app.controller.ts @@ -0,0 +1,15 @@ +import { Controller, Get, UseGuards } from "@nestjs/common"; +import { ApiBearerAuth } from "@nestjs/swagger"; +import { JwtAuthGuard } from "./modules/auth/guards/jwt.guard"; + +@Controller() +@ApiBearerAuth() +@UseGuards(JwtAuthGuard) +export class AppController { + @Get("ping") + pong() { + return { + message: "pong", + }; + } +} diff --git a/src/app.module.ts b/src/app.module.ts index 179f08c..de60341 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -7,6 +7,7 @@ import { envValidation } from "@Validations/env.validation"; import { AuthModule } from "@Modules/auth/auth.module"; import { UserModule } from "@Modules/user/user.module"; import { PrismaModule } from "nestjs-prisma"; +import { AppController } from "./app.controller"; @Module({ imports: [ @@ -31,5 +32,6 @@ import { PrismaModule } from "nestjs-prisma"; UserModule, AuthModule, ], + controllers: [AppController], }) export class AppModule {} diff --git a/src/interfaces/jwtPayload.ts b/src/interfaces/jwtPayload.ts new file mode 100644 index 0000000..8632292 --- /dev/null +++ b/src/interfaces/jwtPayload.ts @@ -0,0 +1,3 @@ +export interface JwtPayload { + id: string; +} diff --git a/src/modules/auth/strategy/jwt.strategy.ts b/src/modules/auth/strategy/jwt.strategy.ts index fc7e2d9..7a2bef5 100644 --- a/src/modules/auth/strategy/jwt.strategy.ts +++ b/src/modules/auth/strategy/jwt.strategy.ts @@ -1,9 +1,10 @@ +import { JwtPayload } from "@/interfaces/jwtPayload"; +import { UserService } from "@Modules/user/user.service"; import { Injectable, UnauthorizedException } from "@nestjs/common"; +import { ConfigService } from "@nestjs/config"; import { PassportStrategy } from "@nestjs/passport"; import { User } from "@prisma/client"; -import { Strategy, ExtractJwt } from "passport-jwt"; -import { UserService } from "@Modules/user/user.service"; -import { ConfigService } from "@nestjs/config"; +import { ExtractJwt, Strategy } from "passport-jwt"; @Injectable() export class JWTStrategy extends PassportStrategy(Strategy, "jwt") { @@ -18,7 +19,7 @@ export class JWTStrategy extends PassportStrategy(Strategy, "jwt") { }); } - async validate(payload: any): Promise { + async validate(payload: JwtPayload): Promise { const user = await this.userService.findById(payload.id); if (!user) { diff --git a/src/modules/auth/strategy/refresh.strategy.ts b/src/modules/auth/strategy/refresh.strategy.ts index 6ed3fbd..e37f79b 100644 --- a/src/modules/auth/strategy/refresh.strategy.ts +++ b/src/modules/auth/strategy/refresh.strategy.ts @@ -1,9 +1,10 @@ +import { JwtPayload } from "@/interfaces/jwtPayload"; +import { UserService } from "@Modules/user/user.service"; import { Injectable, UnauthorizedException } from "@nestjs/common"; +import { ConfigService } from "@nestjs/config"; import { PassportStrategy } from "@nestjs/passport"; import { User } from "@prisma/client"; -import { Strategy, ExtractJwt } from "passport-jwt"; -import { UserService } from "@Modules/user/user.service"; -import { ConfigService } from "@nestjs/config"; +import { ExtractJwt, Strategy } from "passport-jwt"; @Injectable() export class RefreshJWTStrategy extends PassportStrategy(Strategy, "refresh") { @@ -18,7 +19,7 @@ export class RefreshJWTStrategy extends PassportStrategy(Strategy, "refresh") { }); } - async validate(payload: any): Promise { + async validate(payload: JwtPayload): Promise { const user = await this.userService.findById(payload.id); if (!user) { diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index 5e1ae89..d1d1399 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -25,7 +25,7 @@ export class UserService { }); } - async findById(id: string) { + async findById(id: string = null) { return await this.prisma.user.findUnique({ where: { id }, });