mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 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,
|
|
roles: payload?.roles,
|
|
};
|
|
} else {
|
|
throw new BadRequestException('Unauthorized');
|
|
}
|
|
}
|
|
}
|