mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { ConfigService } from '@nestjs/config';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import { UserSessionRepository } from '../../../src/modules/session/repositories/session.repository';
|
|
import { AuthInterface } from '../interfaces/auth.interface';
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|
constructor(
|
|
private readonly sessionRepository: UserSessionRepository,
|
|
private readonly configService: ConfigService,
|
|
) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: configService.get('JWT_SECRET'),
|
|
});
|
|
}
|
|
|
|
async validate(payload: AuthInterface) {
|
|
const validateUser = await this.sessionRepository.findOne({
|
|
where: {
|
|
uuid: payload.sessionId,
|
|
isLoggedOut: false,
|
|
},
|
|
});
|
|
if (validateUser) {
|
|
return {
|
|
email: payload.email,
|
|
userUuid: payload.uuid,
|
|
uuid: payload.uuid,
|
|
sessionId: payload.sessionId,
|
|
role: payload?.role,
|
|
project: payload?.project,
|
|
};
|
|
} else {
|
|
throw new BadRequestException('Unauthorized');
|
|
}
|
|
}
|
|
}
|