fix(auth): ensure JwtStrategy always validates database admin users without false fallback block
This commit is contained in:
parent
ef2ba3ab4a
commit
b98c6bb457
@ -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
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user