Criação do handler de autenticação, para um futuro microserviço de auth
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
export class AuthenticateUserCommand {
|
||||
constructor(
|
||||
public readonly username: string,
|
||||
public readonly password: string,
|
||||
) {}
|
||||
}
|
||||
|
||||
export class AuthenticateUserCommand {
|
||||
constructor(
|
||||
public readonly username: string,
|
||||
public readonly password: string,
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export class AuthenticateUserService {
|
||||
}
|
||||
|
||||
if (user.dataDesligamento !== null) {
|
||||
return Result.fail('Usuário desligado da empresa, login não permitido!');
|
||||
return Result.fail('Usuário desligado da empresa, login não permitido, Procure o setor de RH!');
|
||||
}
|
||||
|
||||
if (user.situacao === 'I') {
|
||||
|
||||
4
src/auth/jwt.config.ts
Normal file
4
src/auth/jwt.config.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export default () => ({
|
||||
jwtSecret: process.env.JWT_SECRET || 'fallback-secret',
|
||||
jwtExpiresIn: process.env.JWT_EXPIRES_IN || '8h',
|
||||
});
|
||||
@@ -1,7 +1,8 @@
|
||||
// ✅ jwt.strategy.ts
|
||||
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
import { JwtPayload } from '../models/jwt-payload.model';
|
||||
import { UserRepository } from '../../auth/users/UserRepository';
|
||||
import { RedisClientToken } from '../../core/configs/cache/redis-client.adapter.provider';
|
||||
@@ -12,32 +13,38 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(
|
||||
@Inject(RedisClientToken) private readonly redis: IRedisClient,
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: '4557C0D7-DFB0-40DA-BF83-91A75103F7A9',
|
||||
secretOrKey: configService.get<string>('jwtSecret'),
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: JwtPayload) {
|
||||
const sessionKey = `session:${payload.id}`;
|
||||
const user = await this.redis.get<any>(sessionKey);
|
||||
const sessionKey = this.buildSessionKey(payload.id);
|
||||
const cachedUser = await this.redis.get<any>(sessionKey);
|
||||
|
||||
if (user) {
|
||||
// Audit log placeholder
|
||||
// await this.auditAccess(user);
|
||||
return user;
|
||||
if (cachedUser) {
|
||||
// await this.auditAccess(cachedUser);
|
||||
return cachedUser;
|
||||
}
|
||||
|
||||
const userDb = await this.userRepository.findById(payload.id);
|
||||
if (!userDb || userDb.situacao === 'I' || userDb.dataDesligamento) {
|
||||
const user = await this.userRepository.findById(payload.id);
|
||||
if (!user || user.situacao === 'I' || user.dataDesligamento) {
|
||||
throw new UnauthorizedException('Usuário inválido ou inativo');
|
||||
}
|
||||
|
||||
await this.redis.set(sessionKey, userDb, 60 * 60 * 8);
|
||||
// Audit fallback
|
||||
// await this.auditAccess(userDb, 'fallback');
|
||||
await this.redis.set(sessionKey, user, 60 * 60 * 8); // 8h
|
||||
|
||||
return userDb;
|
||||
return {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
};
|
||||
}
|
||||
|
||||
private buildSessionKey(userId: number): string {
|
||||
return `auth:sessions:${userId}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user