fix(auth): ensure JwtStrategy always validates database admin users without false fallback block
Some checks failed
Deploy Canina / deploy (push) Successful in 32s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s

This commit is contained in:
پارسا آقایی 2026-09-23 23:32:26 +03:30
parent ef2ba3ab4a
commit b98c6bb457
2 changed files with 12 additions and 28 deletions

View File

@ -414,7 +414,17 @@ export class AuthService {
let userPayload: Record<string, any>;
let safeUser: any;
if (userId === '12345678-1234-1234-1234-123456789012') {
const user = await this.prisma.user.findUnique({ where: { id: userId } });
if (user) {
userPayload = {
sub: user.id,
email: user.email,
phoneNumber: user.mobile,
role: user.role,
};
const { password: _p, ...restUser } = user;
safeUser = restUser;
} else if (userId === '12345678-1234-1234-1234-123456789012') {
if (process.env.NODE_ENV === 'production') {
throw new UnauthorizedException('حساب کاربری مدیریت معتبر نیست');
}
@ -429,18 +439,7 @@ export class AuthService {
role: 'Admin',
};
} else {
const user = await this.prisma.user.findUnique({ where: { id: userId } });
if (!user) {
throw new UnauthorizedException('کاربر یافت نشد');
}
userPayload = {
sub: user.id,
email: user.email,
phoneNumber: user.mobile,
role: user.role,
};
const { password: _p, ...restUser } = user;
safeUser = restUser;
throw new UnauthorizedException('کاربر یافت نشد');
}
// Issue new pair of tokens

View File

@ -36,21 +36,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
throw new UnauthorizedException('این توکن باطل شده است. لطفاً مجدداً وارد شوید');
}
}
// Handle fallback admin ID only in non-production environments if ADMIN_EMAIL matches
if (payload.sub === '12345678-1234-1234-1234-123456789012') {
if (process.env.NODE_ENV === 'production') {
throw new UnauthorizedException('حساب کاربری مدیریت معتبر نیست');
}
const adminEmail = process.env.ADMIN_EMAIL;
if (adminEmail && payload.email === adminEmail) {
return {
id: payload.sub,
email: adminEmail,
role: 'Admin',
};
}
throw new UnauthorizedException('حساب کاربری مدیریت معتبر نیست');
}
const user = await this.usersService.findById(payload.sub);
if (!user) {