ref: just add another test route

This commit is contained in:
M1000fr 2024-12-05 00:12:51 +01:00
parent 8245f4ddfc
commit d9cc0db0d2
6 changed files with 31 additions and 9 deletions

15
src/app.controller.ts Normal file
View File

@ -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",
};
}
}

View File

@ -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 {}

View File

@ -0,0 +1,3 @@
export interface JwtPayload {
id: string;
}

View File

@ -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<User> {
async validate(payload: JwtPayload): Promise<User> {
const user = await this.userService.findById(payload.id);
if (!user) {

View File

@ -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<User> {
async validate(payload: JwtPayload): Promise<User> {
const user = await this.userService.findById(payload.id);
if (!user) {

View File

@ -25,7 +25,7 @@ export class UserService {
});
}
async findById(id: string) {
async findById(id: string = null) {
return await this.prisma.user.findUnique({
where: { id },
});