import { ExtractJwt, Strategy } from 'passport-jwt'; import { PassportStrategy } from '@nestjs/passport'; import { Injectable, UnauthorizedException } from '@nestjs/common'; import { secretKey } from '../../../constant/global.constant'; import { InjectRepository } from '@nestjs/typeorm'; import { UserSession } from 'src/module/user/entity/user-session.entity'; import { BaseEntity, Repository } from 'typeorm'; import { ConfigService } from '@nestjs/config'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(private readonly configService: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.get('SECRET_KEY'), }); } async validate(payload) { const { id, sessionToken, appcode, email_id, organization_id, enterprise_id, level_id, level_type, } = payload; // Check if session exists and is valid // const userSession = await this.userSessionRepository.findOne({ // where: { session_key: sessionToken }, // }); // if ( // !userSession || // userSession.is_session_loggedout || // new Date(userSession.expiry_date) < new Date() // ) { // throw new UnauthorizedException('Invalid or expired session'); // } const userData = { id: id, sessionToken: sessionToken, appcode: appcode, email_id: email_id, organization_id: organization_id, enterprise_id: enterprise_id, level_id: level_id, level_type: level_type, }; return { userData }; } }