Files
Hushian/Hushian.Application/Services/AuthService.cs

133 lines
5.1 KiB
C#
Raw Normal View History

2025-06-29 15:29:51 +03:30
using Common.Models.Auth;
using Common.Models.Auth.CompanySide;
using Common.Models.Auth.UserSide;
2025-07-12 21:33:44 +03:30
using Common.Validation;
2025-06-29 15:29:51 +03:30
using Hushian.Application.Constants;
using Hushian.Application.Contracts.Persistence;
using Hushian.Application.Models;
using Hushian.Domain.Entites;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace Hushian.Application.Services
{
public class AuthService
{
private readonly JwtSettings _jwtSettings;
private readonly IGenericRepository<Company> _CompanyRepository;
private readonly IGenericRepository<User> _UserRepository;
private readonly IGenericRepository<Exper> _ExperRepository;
private readonly VerificationService _verificationService;
2025-07-07 22:04:07 +03:30
public AuthService(IOptions<JwtSettings> jwtSettings
, IGenericRepository<Company> companyRepository
, IGenericRepository<User> userRepository
, IGenericRepository<Exper> experRepository
, VerificationService verificationService)
2025-06-29 15:29:51 +03:30
{
_jwtSettings = jwtSettings.Value;
2025-06-29 16:14:42 +03:30
_CompanyRepository = companyRepository;
_UserRepository = userRepository;
_ExperRepository = experRepository;
_verificationService = verificationService;
2025-06-29 15:29:51 +03:30
}
public async Task<ResponseBase<AuthResponse>> AuthenticationFromCompanySide
(AuthRequestFromCompanySide auth)
{
ResponseBase<AuthResponse> Response = new();
2025-07-03 16:05:44 +03:30
if (!FixedValidation.CheckUsername(auth.Username))
2025-06-29 16:14:42 +03:30
{
2025-07-03 16:05:44 +03:30
Response.Errors.Add("نام کاربری اشتباه است");
2025-06-29 16:14:42 +03:30
}
else
{
2025-07-03 16:05:44 +03:30
if (auth.Username.StartsWith("09"))
2025-06-29 16:14:42 +03:30
{
2025-07-03 16:05:44 +03:30
// in Company Search
2025-07-11 20:37:28 +03:30
var Company = await _CompanyRepository.Get()
.FirstOrDefaultAsync(f => f.Mobile == auth.Username && f.Password == auth.Password.GetHash() && f.Verified);
2025-07-03 16:05:44 +03:30
if (Company == null)
{
Response.Errors.Add("کاربری یافت نشد");
}
else
{
Response.Success = true;
Response.Value = new AuthResponse()
{
Fullname = Company.FullName,
Id = Company.ID,
2025-07-11 20:37:28 +03:30
Role="Company",
img=Company.logo,
2025-07-03 16:05:44 +03:30
MobileOrUserName = Company.Mobile,
2025-07-25 21:29:57 +03:30
CompanyId= Company.ID,
2025-07-24 23:18:11 +03:30
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(Company.Mobile, Company.ID, "Company"))
2025-07-03 16:05:44 +03:30
};
}
2025-06-29 16:14:42 +03:30
}
else
{
2025-07-11 20:37:28 +03:30
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username
&& f.Password == auth.Password.GetHash() && f.Available);
2025-07-03 16:05:44 +03:30
if (exper == null)
{
Response.Errors.Add("کاربری یافت نشد");
}
else
2025-06-29 16:14:42 +03:30
{
2025-07-03 16:05:44 +03:30
Response.Success = true;
Response.Value = new AuthResponse()
{
Fullname = exper.FullName,
Id = exper.ID,
2025-07-11 20:37:28 +03:30
CompanyId = exper.CompanyID,
2025-07-03 16:05:44 +03:30
MobileOrUserName = exper.UserName,
2025-07-11 20:37:28 +03:30
Role="Exper",
2025-07-24 23:18:11 +03:30
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(exper.UserName, exper.ID, "Exper"))
2025-07-03 16:05:44 +03:30
};
}
2025-06-29 16:14:42 +03:30
}
}
2025-07-03 16:05:44 +03:30
2025-06-29 15:29:51 +03:30
return Response;
}
public async Task<ResponseBase<int>> AuthenticationFromUserSide
(AuthRequestFromUserSide auth)
{
ResponseBase<int> Response = new();
2025-07-03 16:05:44 +03:30
if (!FixedValidation.CheckUsername(auth.Mobile))
2025-06-29 15:29:51 +03:30
{
2025-07-03 16:05:44 +03:30
Response.Errors.Add("نام کاربری اشتباه است");
}
else
{
if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile))
2025-06-29 15:29:51 +03:30
{
2025-07-03 16:05:44 +03:30
if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName }))
{
Response.Errors.Add("خطا در کاربری");
}
2025-06-29 15:29:51 +03:30
}
2025-07-03 16:05:44 +03:30
if (Response.Errors.Count == 0)
{
Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile);
Response.Success = true;
}
2025-06-29 15:29:51 +03:30
}
2025-07-03 16:05:44 +03:30
2025-06-29 15:29:51 +03:30
return Response;
}
2025-07-24 23:18:11 +03:30
2025-06-29 15:29:51 +03:30
}
}