Compare commits

...

2 Commits

Author SHA1 Message Date
yotakii
625bc26a05 Merge branch 'master' of https://git.atmata-group.com/ATMATA/zerp 2026-03-05 11:57:04 +03:00
yotakii
8365f4da2d fix login error 2026-03-05 11:54:14 +03:00
3 changed files with 62 additions and 19 deletions

View File

@@ -42,7 +42,7 @@ model AuditLog {
model User {
id String @id @default(uuid())
email String @unique
email String
username String @unique
password String
isActive Boolean @default(true)

View File

@@ -21,8 +21,18 @@ export const authController = {
login: async (req: Request, res: Response) => {
try {
const { email, password } = req.body
if (!email || !password) {
return res.status(400).json({
success: false,
message: 'الرجاء إدخال البريد/اسم المستخدم وكلمة المرور'
})
}
const result = await authService.login(email, password)
res.status(200).json({
success: true,
message: 'تم تسجيل الدخول بنجاح',

View File

@@ -47,26 +47,60 @@ class AuthService {
};
}
async login(email: string, password: string) {
// Find user with employee info and permissions
const user = await prisma.user.findUnique({
where: { email },
include: {
employee: {
include: {
position: {
include: {
permissions: true,
const identifier = (email || '').toString().trim();
const isEmail = identifier.includes('@');
let user: any = null;
if (isEmail) {
// email may be duplicated => use findMany and validate
const users = await prisma.user.findMany({
where: { email: identifier },
include: {
employee: {
include: {
position: {
include: { permissions: true },
},
department: true,
},
department: true,
},
},
},
});
});
if (!user) {
throw new AppError(401, 'بيانات الدخول غير صحيحة - Invalid credentials');
if (users.length === 0) {
throw new AppError(401, 'بيانات الدخول غير صحيحة - Invalid credentials');
}
if (users.length > 1) {
throw new AppError(
400,
'هذا البريد مستخدم لأكثر من حساب. الرجاء تسجيل الدخول باسم المستخدم - Email shared, use username'
);
}
user = users[0];
} else {
// username is unique => findUnique OK
user = await prisma.user.findUnique({
where: { username: identifier },
include: {
employee: {
include: {
position: {
include: { permissions: true },
},
department: true,
},
},
},
});
if (!user) {
throw new AppError(401, 'بيانات الدخول غير صحيحة - Invalid credentials');
}
}
// Check if user is active
@@ -84,7 +118,7 @@ class AuthService {
if (!isPasswordValid) {
// Increment failed login attempts
const failedAttempts = user.failedLoginAttempts + 1;
const failedAttempts = (user.failedLoginAttempts || 0) + 1;
const updateData: any = { failedLoginAttempts: failedAttempts };
// Lock account after 5 failed attempts
@@ -277,4 +311,3 @@ class AuthService {
}
export const authService = new AuthService();