ref: just add another test route
This commit is contained in:
parent
8245f4ddfc
commit
d9cc0db0d2
15
src/app.controller.ts
Normal file
15
src/app.controller.ts
Normal 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",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ import { envValidation } from "@Validations/env.validation";
|
|||||||
import { AuthModule } from "@Modules/auth/auth.module";
|
import { AuthModule } from "@Modules/auth/auth.module";
|
||||||
import { UserModule } from "@Modules/user/user.module";
|
import { UserModule } from "@Modules/user/user.module";
|
||||||
import { PrismaModule } from "nestjs-prisma";
|
import { PrismaModule } from "nestjs-prisma";
|
||||||
|
import { AppController } from "./app.controller";
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -31,5 +32,6 @@ import { PrismaModule } from "nestjs-prisma";
|
|||||||
UserModule,
|
UserModule,
|
||||||
AuthModule,
|
AuthModule,
|
||||||
],
|
],
|
||||||
|
controllers: [AppController],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
3
src/interfaces/jwtPayload.ts
Normal file
3
src/interfaces/jwtPayload.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export interface JwtPayload {
|
||||||
|
id: string;
|
||||||
|
}
|
@ -1,9 +1,10 @@
|
|||||||
|
import { JwtPayload } from "@/interfaces/jwtPayload";
|
||||||
|
import { UserService } from "@Modules/user/user.service";
|
||||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
import { PassportStrategy } from "@nestjs/passport";
|
import { PassportStrategy } from "@nestjs/passport";
|
||||||
import { User } from "@prisma/client";
|
import { User } from "@prisma/client";
|
||||||
import { Strategy, ExtractJwt } from "passport-jwt";
|
import { ExtractJwt, Strategy } from "passport-jwt";
|
||||||
import { UserService } from "@Modules/user/user.service";
|
|
||||||
import { ConfigService } from "@nestjs/config";
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class JWTStrategy extends PassportStrategy(Strategy, "jwt") {
|
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);
|
const user = await this.userService.findById(payload.id);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
|
import { JwtPayload } from "@/interfaces/jwtPayload";
|
||||||
|
import { UserService } from "@Modules/user/user.service";
|
||||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
import { PassportStrategy } from "@nestjs/passport";
|
import { PassportStrategy } from "@nestjs/passport";
|
||||||
import { User } from "@prisma/client";
|
import { User } from "@prisma/client";
|
||||||
import { Strategy, ExtractJwt } from "passport-jwt";
|
import { ExtractJwt, Strategy } from "passport-jwt";
|
||||||
import { UserService } from "@Modules/user/user.service";
|
|
||||||
import { ConfigService } from "@nestjs/config";
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RefreshJWTStrategy extends PassportStrategy(Strategy, "refresh") {
|
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);
|
const user = await this.userService.findById(payload.id);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
@ -25,7 +25,7 @@ export class UserService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async findById(id: string) {
|
async findById(id: string = null) {
|
||||||
return await this.prisma.user.findUnique({
|
return await this.prisma.user.findUnique({
|
||||||
where: { id },
|
where: { id },
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user