AuthController work

This commit is contained in:
Artemy
2025-06-18 16:55:19 +07:00
parent 2c2c2b805c
commit 7d518c0882
26 changed files with 397 additions and 63 deletions
@@ -0,0 +1,32 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Govor.Core.Models;
using Govor.Core.Services;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace Govor.API.Services.Authentication;
public class JwtService : IJwtService
{
private JwtOption _jwtOption;
public JwtService(IOptions<JwtOption> options)
{
_jwtOption = options.Value;
}
public string GenerateJwtToken(User user)
{
Claim[] claims = [new("userID", user.Id.ToString())];
var singing = new SigningCredentials(
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOption.SecretKeу)),
SecurityAlgorithms.HmacSha256Signature);
var token = new JwtSecurityToken(signingCredentials: singing,
expires: DateTime.UtcNow.AddHours(_jwtOption.Hours));
return new JwtSecurityTokenHandler().WriteToken(token);
}
}