This commit is contained in:
Joelson
2025-09-17 18:49:23 -03:00
parent 21c3225c52
commit e081df9ced
42 changed files with 4129 additions and 411 deletions

View File

@@ -31,17 +31,34 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
throw new UnauthorizedException('Token foi invalidado');
}
const sessionKey = this.buildSessionKey(payload.id);
/**
* Usa a mesma chave que o SessionManagementService
* Formato: auth:sessions:userId:sessionId
*/
const sessionKey = this.buildSessionKey(payload.id, payload.sessionId);
const cachedUser = await this.redis.get<any>(sessionKey);
if (cachedUser) {
/**
* Verifica se a sessão ainda está ativa
*/
const isSessionActive = await this.sessionManagementService.isSessionActive(
payload.id,
payload.sessionId
);
if (!isSessionActive) {
throw new UnauthorizedException('Sessão expirada ou inválida');
}
return {
id: cachedUser.id,
sellerId: cachedUser.sellerId,
storeId: cachedUser.storeId,
username: cachedUser.name,
username: cachedUser.username, // ← Corrigido: usar username em vez de name
email: cachedUser.email,
name: cachedUser.name,
sessionId: payload.sessionId,
};
}
@@ -50,14 +67,21 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
throw new UnauthorizedException('Usuário inválido ou inativo');
}
/**
* Verifica se usuário está bloqueado (consistência com AuthenticateUserHandler)
*/
if (user.situacao === 'B') {
throw new UnauthorizedException('Usuário bloqueado, acesso não permitido');
}
const userData = {
id: user.id,
sellerId: user.sellerId,
storeId: user.storeId,
username: user.name,
username: user.name, // ← Manter name como username para compatibilidade
email: user.email,
name: user.name,
sessionId: payload.sessionId, // Inclui sessionId do token
sessionId: payload.sessionId,
};
await this.redis.set(sessionKey, userData, 60 * 60 * 8);
@@ -65,7 +89,13 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
return userData;
}
private buildSessionKey(userId: number): string {
return `auth:sessions:${userId}`;
/**
* Constrói a chave de sessão no mesmo formato do SessionManagementService
* @param userId ID do usuário
* @param sessionId ID da sessão
* @returns Chave para o Redis
*/
private buildSessionKey(userId: number, sessionId: string): string {
return `auth:sessions:${userId}:${sessionId}`;
}
}