Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authservice fixed #166

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/HaadCRM.Service/Services/Assets/AssetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async ValueTask<AssetViewModel> UploadAsync(IFormFile file, FileType type
return mapper.Map<AssetViewModel>(asset);
}


public async ValueTask<bool> DeleteAsync(long id)
{
var asset = await unitOfWork.Assets.SelectAsync(a => a.Id == id)
Expand Down
29 changes: 16 additions & 13 deletions src/HaadCRM.Service/Services/AuthServices/AuthService.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
using HaadCRM.Domain.Entities.Users;
using HaadCRM.Service.Services.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace HaadCRM.Service.Services.AuthServices;

public class AuthService(
IConfiguration configuration) : IAuthService
public class AuthService(IOptions<JwtOption> options) : IAuthService
{
private readonly JwtOption jwtOption = options.Value;
public string GenerateToken(User user)
{
var tokenHandler = new JwtSecurityTokenHandler();
var tokenKey = Encoding.UTF8.GetBytes(configuration["JWT:Key"]);
var tokenDescriptor = new SecurityTokenDescriptor
var tokenKey = Encoding.UTF8.GetBytes(jwtOption.Key);
var tokenDescriptor = new SecurityTokenDescriptor()
{
Audience = jwtOption.Audience,
Expires = DateTime.UtcNow.AddHours(Convert.ToInt32(jwtOption.LifeTime)),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(tokenKey), SecurityAlgorithms.HmacSha256Signature),
Issuer = jwtOption.Issuer,
Subject = new ClaimsIdentity(new Claim[]
{
new Claim("Phone", user.Phone),
new Claim("Id", user.Id.ToString()),
new Claim(ClaimTypes.Role, user.UserRole.Name)
}),
Expires = DateTime.UtcNow.AddHours(1),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(tokenKey), SecurityAlgorithms.HmacSha256Signature)
new("Id", user.Id.ToString()),
new("Phone", user.Phone),
new(ClaimTypes.Role, user.UserRole.Name)
})
};
var token = tokenHandler.CreateToken(tokenDescriptor);

var result = tokenHandler.WriteToken(token);
var token = tokenHandler.CreateToken(tokenDescriptor);

return result;
return tokenHandler.WriteToken(token);
}
}

4 changes: 3 additions & 1 deletion src/HaadCRM.Service/Services/Users/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HaadCRM.Service.Exceptions;
using HaadCRM.Service.Extensions;
using HaadCRM.Service.Helpers;
using HaadCRM.Service.Services.AuthServices;
using HaadCRM.Service.Validators.Users.Users;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
Expand All @@ -17,6 +18,7 @@ public class UserService(
IUnitOfWork unitOfWork,
IMapper mapper,
IMemoryCache memoryCache,
IAuthService authService,
UserCreateModelValidator userCreateModelValidator,
UserUpdateModelValidator userUpdateModelValidator) : IUserService
{
Expand Down Expand Up @@ -124,7 +126,7 @@ public async ValueTask<UserViewModel> GetByIdAsync(long id)
if (!isVerified)
throw new ArgumentIsNotValidException("Phone or password is not valid");
// Login successful, return user and generated token
var token = AuthHelper.GenerateToken(existingUser);
var token = authService.GenerateToken(existingUser);
return (existingUser, token);
}

Expand Down
4 changes: 4 additions & 0 deletions src/HaadCRM.WebApi/HaadCRM.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<ProjectReference Include="..\HaadCRM.Service\HaadCRM.Service.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

</Project>