103 lines
3.7 KiB
C#
103 lines
3.7 KiB
C#
![]() |
using Common.Contracts.Infrastructure;
|
|||
|
using Common.Dtos.Verification;
|
|||
|
using Common.Enums;
|
|||
|
using Common.Models.Auth;
|
|||
|
using Hushian.Application.Contracts.Persistence;
|
|||
|
using Hushian.Application.Models;
|
|||
|
using Hushian.Domain.Entites;
|
|||
|
using Identity.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IdentityModel.Tokens.Jwt;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Hushian.Application.Services
|
|||
|
{
|
|||
|
public class VerificationService
|
|||
|
{
|
|||
|
private readonly IGenericRepository<VerificationCode> _VerificationCodeRepository;
|
|||
|
private readonly IMessageSender _messageSender;
|
|||
|
private readonly IGenericRepository<User> _UserRepository;
|
|||
|
private readonly AuthService _authService;
|
|||
|
public async Task<int> GenerateCodeForLoginUser(string Mobile)
|
|||
|
{
|
|||
|
string Code = await GenerateCode();
|
|||
|
var response= await _VerificationCodeRepository.ADD
|
|||
|
(new Identity.Models.VerificationCode(VerificationCodeType.Login, Code, Mobile));
|
|||
|
await _messageSender.SendMassage(new Models.Message.Message()
|
|||
|
{
|
|||
|
msg = Code,
|
|||
|
To = Mobile
|
|||
|
});
|
|||
|
return response.ID;
|
|||
|
}
|
|||
|
public async Task<ResponseBase<AuthResponse>> VerificationCode(ConfirmedCodeDto model)
|
|||
|
{
|
|||
|
var response = new ResponseBase<AuthResponse>();
|
|||
|
var resultConf=await _VerificationCodeRepository.Get()
|
|||
|
.FirstOrDefaultAsync(w => w.ID == model.Id && w.Code == model.code && w.Type == model.codeType);
|
|||
|
if (resultConf!=null)
|
|||
|
{
|
|||
|
if (resultConf.Type == VerificationCodeType.Login)
|
|||
|
{
|
|||
|
var User= await _UserRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile);
|
|||
|
if (User!=null)
|
|||
|
{
|
|||
|
response.Success = true;
|
|||
|
response.Value = new AuthResponse()
|
|||
|
{
|
|||
|
Fullname = User.FullName,
|
|||
|
Id = User.ID,
|
|||
|
MobileOrUserName = User.Mobile,
|
|||
|
Token = new JwtSecurityTokenHandler().WriteToken(await _authService.GenerateToken(User.Mobile, User.ID))
|
|||
|
};
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
response.Errors.Add("کاربری یافت نشد");
|
|||
|
}
|
|||
|
}
|
|||
|
else if (resultConf.Type == VerificationCodeType.ForgetPassword)
|
|||
|
{
|
|||
|
if ((string.IsNullOrEmpty(model.value) || model.value.Length < 5))
|
|||
|
response.Errors.Add("کلمه عبور باید بیشتر از 5 کاراکتر باشد");
|
|||
|
else
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
else if (resultConf.Type == VerificationCodeType.PhoneNumberConfirmed)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
response.Errors.Add("احراز صحیح نمی باشد");
|
|||
|
}
|
|||
|
|
|||
|
await _VerificationCodeRepository.DELETE(resultConf);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
response.Errors.Add("احراز یافت نشد");
|
|||
|
}
|
|||
|
|
|||
|
return response;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
private async Task<string> GenerateCode()
|
|||
|
{
|
|||
|
int Code = Random.Shared.Next(1000, 9000);
|
|||
|
while (await _VerificationCodeRepository.Get().AnyAsync(w => w.Code == Code.ToString()))
|
|||
|
Code = Random.Shared.Next(1000, 9000);
|
|||
|
|
|||
|
return Code.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|