Skip to content

Commit

Permalink
update files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohirjon-Odilov committed Mar 4, 2024
1 parent bb4a6a5 commit b88b835
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -25,34 +27,29 @@ public AuthService(IConfiguration conf, IUserService userService)

public async Task<string> 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!";
}
throw new NotFoundException();
}

public async Task<ResponseLogin> GenerateToken(CheckEmail user, string path)
public async Task<ResponseLogin> 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<int> permissionsId = new List<int>();
if (result.Role == "Admin")
Expand All @@ -65,8 +62,8 @@ public async Task<ResponseLogin> GenerateToken(CheckEmail user, string path)

List<Claim> claims = new List<Claim>()
{
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)
Expand All @@ -77,8 +74,9 @@ public async Task<ResponseLogin> GenerateToken(CheckEmail user, string path)

public async Task<ResponseLogin> GenerateToken(IEnumerable<Claim> 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");

Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public class ResponseLogin
{
public string Token { get; set; }
public string? Token { get; set; }
}
}

0 comments on commit b88b835

Please sign in to comment.