31 lines
840 B
TypeScript
31 lines
840 B
TypeScript
import { Strategy } from 'passport-jwt';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
export type JwtPayload = { userId: string; workspaceId: string };
|
|
|
|
@Injectable()
|
|
export class JwtAuthStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|
constructor(configService: ConfigService) {
|
|
const extractJwtFromCookie = (req) => {
|
|
let token = null;
|
|
|
|
if (req && req.cookies) {
|
|
token = req.cookies['jwt'];
|
|
}
|
|
return token;
|
|
};
|
|
|
|
super({
|
|
jwtFromRequest: extractJwtFromCookie,
|
|
ignoreExpiration: false,
|
|
secretOrKey: configService.get<string>('JWT_SECRET'),
|
|
});
|
|
}
|
|
|
|
async validate(payload: JwtPayload) {
|
|
return { userId: payload.userId, workspaceId: payload.workspaceId };
|
|
}
|
|
}
|