This commit is contained in:
mmrbnjd
2025-07-03 16:05:44 +03:30
parent bc65878608
commit 87d2360b32
8 changed files with 338 additions and 51 deletions

View File

@@ -4,6 +4,7 @@ using Common.Models.Auth.UserSide;
using Hushian.Application.Constants;
using Hushian.Application.Contracts.Persistence;
using Hushian.Application.Models;
using Hushian.Application.Validation;
using Hushian.Domain.Entites;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
@@ -38,65 +39,81 @@ namespace Hushian.Application.Services
(AuthRequestFromCompanySide auth)
{
ResponseBase<AuthResponse> Response = new();
if (auth.Username.StartsWith("09"))
if (!FixedValidation.CheckUsername(auth.Username))
{
// in Company Search
var Company= await _CompanyRepository.Get().FirstOrDefaultAsync(f=>f.Mobile== auth.Username && f.Password==auth.Password.GetHash());
if (Company==null)
{
Response.Errors.Add("کاربری یافت نشد");
}
else
{
Response.Success = true;
Response.Value = new AuthResponse()
{
Fullname = Company.FullName,
Id = Company.ID,
MobileOrUserName = Company.Mobile,
Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(Company.Mobile, Company.ID))
};
}
Response.Errors.Add("نام کاربری اشتباه است");
}
else
{
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username && f.Password == auth.Password.GetHash());
if (exper == null)
if (auth.Username.StartsWith("09"))
{
Response.Errors.Add("کاربری یافت نشد");
// in Company Search
var Company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.Mobile == auth.Username && f.Password == auth.Password.GetHash());
if (Company == null)
{
Response.Errors.Add("کاربری یافت نشد");
}
else
{
Response.Success = true;
Response.Value = new AuthResponse()
{
Fullname = Company.FullName,
Id = Company.ID,
MobileOrUserName = Company.Mobile,
Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(Company.Mobile, Company.ID))
};
}
}
else
{
Response.Success = true;
Response.Value = new AuthResponse()
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username && f.Password == auth.Password.GetHash());
if (exper == null)
{
Fullname = exper.FullName,
Id = exper.ID,
MobileOrUserName = exper.UserName,
Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(exper.UserName, exper.ID))
};
Response.Errors.Add("کاربری یافت نشد");
}
else
{
Response.Success = true;
Response.Value = new AuthResponse()
{
Fullname = exper.FullName,
Id = exper.ID,
MobileOrUserName = exper.UserName,
Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(exper.UserName, exper.ID))
};
}
}
}
return Response;
}
public async Task<ResponseBase<int>> AuthenticationFromUserSide
(AuthRequestFromUserSide auth)
{
ResponseBase<int> Response = new();
if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile))
if (!FixedValidation.CheckUsername(auth.Mobile))
{
if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName }))
Response.Errors.Add("نام کاربری اشتباه است");
}
else
{
if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile))
{
Response.Errors.Add("خطا در کاربری");
if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName }))
{
Response.Errors.Add("خطا در کاربری");
}
}
if (Response.Errors.Count == 0)
{
Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile);
Response.Success = true;
}
}
if (Response.Errors.Count==0)
{
Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile);
Response.Success = true;
}
return Response;
}