diff --git a/eventz-api/Eventz.Application/Dtos/LoginDto.cs b/eventz-api/Eventz.Application/Dtos/LoginDto.cs index 09bd772..ecaeee7 100644 --- a/eventz-api/Eventz.Application/Dtos/LoginDto.cs +++ b/eventz-api/Eventz.Application/Dtos/LoginDto.cs @@ -6,7 +6,7 @@ namespace Eventz.Application.Dtos { - internal class LoginDto + public class LoginDto { public string Email { get; set; } public string Password { get; set; } diff --git a/eventz-api/Eventz.Application/Interfaces/IAuth.cs b/eventz-api/Eventz.Application/Interfaces/IAuth.cs index 113f1fb..7b1a888 100644 --- a/eventz-api/Eventz.Application/Interfaces/IAuth.cs +++ b/eventz-api/Eventz.Application/Interfaces/IAuth.cs @@ -11,6 +11,8 @@ namespace Eventz.Application.Interfaces public interface IAuth { public Task RegisterUser(RegisterDto registerDto); + + public Task LoginUser(LoginDto loginDto); } } diff --git a/eventz-api/Eventz.Application/Interfaces/IToken.cs b/eventz-api/Eventz.Application/Interfaces/IToken.cs new file mode 100644 index 0000000..6e97198 --- /dev/null +++ b/eventz-api/Eventz.Application/Interfaces/IToken.cs @@ -0,0 +1,14 @@ +using Eventz.Domain.Entitites; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Eventz.Application.Interfaces +{ + public interface IToken + { + string GenerateAcesstoken(User user); + } +} diff --git a/eventz-api/Eventz.Infrastructure/Eventz.Infrastructure.csproj b/eventz-api/Eventz.Infrastructure/Eventz.Infrastructure.csproj index fbcd8fe..2b2a22d 100644 --- a/eventz-api/Eventz.Infrastructure/Eventz.Infrastructure.csproj +++ b/eventz-api/Eventz.Infrastructure/Eventz.Infrastructure.csproj @@ -17,6 +17,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs b/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs index 7565b8c..a6b1f42 100644 --- a/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs +++ b/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs @@ -36,5 +36,11 @@ public async Task RegisterUser (RegisterDto registerDto) }; } + + + public async Task LoginUser () + { + return; + } } } diff --git a/eventz-api/Eventz.Infrastructure/Repositories/Auth/TokenRepository.cs b/eventz-api/Eventz.Infrastructure/Repositories/Auth/TokenRepository.cs new file mode 100644 index 0000000..d2e393d --- /dev/null +++ b/eventz-api/Eventz.Infrastructure/Repositories/Auth/TokenRepository.cs @@ -0,0 +1,49 @@ +using Eventz.Application.Interfaces; +using Eventz.Domain.Entitites; +using Microsoft.Extensions.Configuration; +using Microsoft.IdentityModel.Tokens; +using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; +using System.Text; +using System.Threading.Tasks; + +namespace Eventz.Infrastructure.Repositories.Auth +{ + public class TokenRepository : IToken + { + private IConfiguration _configuration; + public TokenRepository(IConfiguration configuration) + { + _configuration = configuration; + } + + public string GenerateAcesstoken(User user) + { + var claims = new[] + { + new Claim(JwtRegisteredClaimNames.Sub, user.UserToken.ToString()), + new Claim(JwtRegisteredClaimNames.Email, user.Email.ToString()), + new Claim(JwtRegisteredClaimNames.Name, user.UserName) + }; + + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]!)); + + var credentials = new SigningCredentials( + key, + SecurityAlgorithms.HmacSha256); + + var token = new JwtSecurityToken( + issuer: _configuration["Jwt:Issuer"], + audience: _configuration["Jwt:Audience"], + claims: claims, + expires: DateTime.UtcNow.AddMinutes( + int.Parse(_configuration["Jwt:ExpiryMinutes"]!)), + signingCredentials: credentials); + + return new JwtSecurityTokenHandler().WriteToken(token); + } + } +}