From b88b835d4fcfa750632db5e49ea768588a20555c Mon Sep 17 00:00:00 2001 From: Tohirjon-Odilov Date: Mon, 4 Mar 2024 12:08:12 +0500 Subject: [PATCH] update files --- .../Services/AuthServices/AuthService.cs | 30 +++++++++---------- .../Entities/DTOs/Auth/RequestSignUp.cs | 3 -- .../Entities/DTOs/Auth/ResponseLogin.cs | 2 +- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Exam.StockManagement.Application/Services/AuthServices/AuthService.cs b/Exam.StockManagement.Application/Services/AuthServices/AuthService.cs index 0fdbee5..efd22ca 100644 --- a/Exam.StockManagement.Application/Services/AuthServices/AuthService.cs +++ b/Exam.StockManagement.Application/Services/AuthServices/AuthService.cs @@ -1,4 +1,6 @@ -using Exam.StockManagement.Application.Abstractions.IServices; +// Ignore Spelling: conf + +using Exam.StockManagement.Application.Abstractions.IServices; using Exam.StockManagement.Domain.Entities.DTOs.Auth; using Exam.StockManagement.Domain.Entities.Models; using Exam.StockManagement.Domain.Exceptions; @@ -14,7 +16,7 @@ namespace Exam.StockManagement.Application.Services.AuthServices { public class AuthService : IAuthService { - private readonly IConfiguration _conf; + private readonly IConfiguration? _conf; private readonly IUserService _userService; public AuthService(IConfiguration conf, IUserService userService) @@ -25,7 +27,7 @@ public AuthService(IConfiguration conf, IUserService userService) public async Task CorrectEmail(RegisterLogin user) { - var result = await _userService.GetByEmail(user.Email); + var result = await _userService.GetByEmail(user.Email!); if (result.Code == user.Code) { return "Login successfully!"; @@ -33,26 +35,21 @@ public async Task CorrectEmail(RegisterLogin user) throw new NotFoundException(); } - public async Task GenerateToken(CheckEmail user, string path) + public async Task GenerateToken(CheckEmail model, string path) { var login = new RequestLogin() { - Email = user.Email, + Email = model.Email, }; - if (File.ReadAllText(path) != user.Code && await UserExist(login)) + if (File.ReadAllText(path) != model.Code && await UserExist(login)) { throw new PasswordNotMatchException(); } File.WriteAllText(path, ""); - if (user is null) - { - throw new NotFoundException(); - } - - var result = await _userService.GetByEmail(user.Email); + var result = await _userService.GetByEmail(model.Email); IEnumerable permissionsId = new List(); if (result.Role == "Admin") @@ -65,8 +62,8 @@ public async Task GenerateToken(CheckEmail user, string path) List claims = new List() { - new Claim(ClaimTypes.Role, result.Role), - new Claim("Login", user.Email), + new Claim(ClaimTypes.Role, result.Role!), + new Claim("Login", model.Email), new Claim("UserID", result.Id.ToString()), new Claim("CreatedDate", DateTime.UtcNow.ToString()), new Claim("permissions",permmisionJson) @@ -77,8 +74,9 @@ public async Task GenerateToken(CheckEmail user, string path) public async Task GenerateToken(IEnumerable additionalClaims) { - var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_conf["JWT:Secret"])); - var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); + SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_conf["JWT:Secret"]!)); + + SigningCredentials credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); var exDate = Convert.ToInt32(_conf["JWT:ExpireDate"] ?? "10"); diff --git a/Exam.StockManagement.Domain/Entities/DTOs/Auth/RequestSignUp.cs b/Exam.StockManagement.Domain/Entities/DTOs/Auth/RequestSignUp.cs index 98cc396..cc5e229 100644 --- a/Exam.StockManagement.Domain/Entities/DTOs/Auth/RequestSignUp.cs +++ b/Exam.StockManagement.Domain/Entities/DTOs/Auth/RequestSignUp.cs @@ -1,11 +1,8 @@ -//using DataAnnotationsExtensions; - namespace Exam.StockManagement.Domain.Entities.DTOs.Auth { public class RequestSignUp { public required string? Name { get; set; } - //[Email] public required string? Email { get; set; } public required string Password { get; set; } public required string ConfirmPassword { get; set; } diff --git a/Exam.StockManagement.Domain/Entities/DTOs/Auth/ResponseLogin.cs b/Exam.StockManagement.Domain/Entities/DTOs/Auth/ResponseLogin.cs index 4c5a1e6..dda4c24 100644 --- a/Exam.StockManagement.Domain/Entities/DTOs/Auth/ResponseLogin.cs +++ b/Exam.StockManagement.Domain/Entities/DTOs/Auth/ResponseLogin.cs @@ -2,6 +2,6 @@ { public class ResponseLogin { - public string Token { get; set; } + public string? Token { get; set; } } }