2022-10-09 22:30:32 +02:00
|
|
|
import { Injectable } from "@nestjs/common";
|
|
|
|
|
import { PassportStrategy } from "@nestjs/passport";
|
|
|
|
|
import { User } from "@prisma/client";
|
2023-01-04 11:54:28 +01:00
|
|
|
import { Request } from "express";
|
|
|
|
|
import { Strategy } from "passport-jwt";
|
2022-11-28 15:04:32 +01:00
|
|
|
import { ConfigService } from "src/config/config.service";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { PrismaService } from "src/prisma/prisma.service";
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
|
|
|
constructor(config: ConfigService, private prisma: PrismaService) {
|
2023-03-04 23:29:00 +01:00
|
|
|
config.get("internal.jwtSecret");
|
2022-10-09 22:30:32 +02:00
|
|
|
super({
|
2023-01-04 11:54:28 +01:00
|
|
|
jwtFromRequest: JwtStrategy.extractJWT,
|
2023-03-04 23:29:00 +01:00
|
|
|
secretOrKey: config.get("internal.jwtSecret"),
|
2022-10-09 22:30:32 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 11:54:28 +01:00
|
|
|
private static extractJWT(req: Request) {
|
|
|
|
|
if (!req.cookies.access_token) return null;
|
|
|
|
|
return req.cookies.access_token;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 23:57:37 +02:00
|
|
|
async validate(payload: { sub: string }) {
|
2022-10-09 22:30:32 +02:00
|
|
|
const user: User = await this.prisma.user.findUnique({
|
|
|
|
|
where: { id: payload.sub },
|
|
|
|
|
});
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|