-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from TomasSirotek/feature/user-login
Feature/user login
- Loading branch information
Showing
72 changed files
with
1,527 additions
and
824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Diagnostics; | ||
using SkillSphere.Application.Common.Interfaces; | ||
using SkillSphere.Application.Common.Models; | ||
|
||
namespace SkillSphere.Application.Auth.Commands; | ||
|
||
public record AuthUserCommand : IRequest<AuthResult> | ||
{ | ||
public string? Email { get; init; } | ||
public string? Password { get; init; } | ||
} | ||
|
||
public class AuthenticateCommandHandler : IRequestHandler<AuthUserCommand,AuthResult> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly IIdentityService _userService; | ||
|
||
public AuthenticateCommandHandler(IApplicationDbContext context,IIdentityService userService) | ||
{ | ||
_context = context; | ||
_userService = userService; | ||
} | ||
public async Task<AuthResult> Handle(AuthUserCommand request, CancellationToken cancellationToken) | ||
{ | ||
// Assuming request.Email and request.Password are provided | ||
|
||
Guard.Against.NullOrEmpty(request.Email, nameof(request.Email)); | ||
Guard.Against.NullOrEmpty(request.Password, nameof(request.Password)); | ||
|
||
return await _userService.AuthenticateAsync(request.Email, request.Password); | ||
//return await _userService.AuthenticateAsync(request.Email, request.Password); | ||
|
||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Diagnostics; | ||
using SkillSphere.Application.Common.Interfaces; | ||
using SkillSphere.Application.Common.Models; | ||
|
||
namespace SkillSphere.Application.Auth.Commands; | ||
|
||
public record RegisterUserCommand : IRequest<Result> | ||
{ | ||
public string? Email { get; init; } | ||
public string? Password { get; init; } | ||
} | ||
|
||
public class RegisterUserCommandHandler : IRequestHandler<RegisterUserCommand,Result> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly IIdentityService _userService; | ||
|
||
public RegisterUserCommandHandler(IApplicationDbContext context,IIdentityService userService) | ||
{ | ||
_context = context; | ||
_userService = userService; | ||
} | ||
public async Task<Result> Handle(RegisterUserCommand request, CancellationToken cancellationToken) | ||
{ | ||
Guard.Against.NullOrEmpty(request.Email, nameof(request.Email)); | ||
Guard.Against.NullOrEmpty(request.Password, nameof(request.Password)); | ||
|
||
var result = await _userService.CreateUserAsync(request.Email, request.Password); | ||
return result; | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace SkillSphere.Application.Common.Models; | ||
|
||
public class AuthResult | ||
{ | ||
public string? Token { get; init; } | ||
|
||
public int ExpiresIn { get; init; } | ||
public string? UserId { get; init; } | ||
public string? Email { get; init; } | ||
|
||
} |
7 changes: 6 additions & 1 deletion
7
...ication/Features/WeatherForecasts/Queries/GetWeatherForecasts/GetWeatherForecastsQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace SkillSphere.Infrastructure.Authentication | ||
{ | ||
|
||
public class JwtTokenConfig | ||
{ | ||
public string Secret { get; init; } = ""; | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace SkillSphere.Infrastructure.Authentication.Model; | ||
|
||
public record TokenModel | ||
{ | ||
public string TokenType { get; } | ||
public string AccessToken { get; } | ||
public DateTime ExpiresAt { get; } | ||
|
||
public TokenModel(string tokenType, string accessToken, DateTime expiresAt) | ||
=> (TokenType, AccessToken, ExpiresAt) = (tokenType, accessToken, expiresAt); | ||
|
||
public int GetRemainingLifetimeSeconds() | ||
=> Math.Max(0, (int)(ExpiresAt - DateTime.Now).TotalSeconds); | ||
} | ||
|
60 changes: 60 additions & 0 deletions
60
src/Infrastructure/Authentication/Services/JwtGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Diagnostics; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Microsoft.IdentityModel.Tokens; | ||
using SkillSphere.Infrastructure.Authentication.Model; | ||
using SkillSphere.Infrastructure.Identity; | ||
|
||
namespace SkillSphere.Infrastructure.Authentication.Services; | ||
|
||
|
||
public interface IJwtTokenGen | ||
{ | ||
ValueTask<string> CreateToken(ApplicationUser user, CancellationToken cancellationToken = default); | ||
} | ||
|
||
public class JwtGenerator : IJwtTokenGen | ||
{ | ||
private readonly JwtTokenConfig _authSettings; | ||
|
||
public JwtGenerator(JwtTokenConfig authSettings) | ||
{ | ||
_authSettings = authSettings; | ||
} | ||
|
||
public ValueTask<string> CreateToken(ApplicationUser user, CancellationToken cancellationToken = default) | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
|
||
Debug.Assert(_authSettings.Secret != null, "_authSettings.Secret != null"); | ||
var key = Encoding.UTF8.GetBytes(_authSettings.Secret); | ||
|
||
Debug.Assert(user.Email != null, "user.Email != null"); | ||
Debug.Assert(user.UserName != null, "user.UserName != null"); | ||
|
||
var claimList = new List<Claim> | ||
{ | ||
new Claim(JwtRegisteredClaimNames.Email, user.Email), | ||
new Claim(JwtRegisteredClaimNames.Sub, user.Id), | ||
new Claim(JwtRegisteredClaimNames.Name, user.UserName) | ||
}; | ||
|
||
// claimList.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role))); | ||
|
||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Audience = null, | ||
Issuer = null, | ||
Subject = new ClaimsIdentity(claimList), | ||
Expires = DateTime.UtcNow.AddSeconds(20), | ||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) | ||
}; | ||
|
||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
var jwtToken = tokenHandler.WriteToken(token); | ||
|
||
return new ValueTask<string>(jwtToken); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using SkillSphere.Infrastructure.Identity; | ||
|
||
namespace SkillSphere.Infrastructure.Data; | ||
|
||
public class InitialData | ||
{ | ||
public static List<ApplicationUser> Users { get; } | ||
|
||
static InitialData() | ||
{ | ||
Users = new List<ApplicationUser> | ||
{ | ||
new ApplicationUser | ||
{ | ||
Id = Guid.NewGuid().ToString(), | ||
UserName = "admin", | ||
Email = "admin@gmail.com", | ||
SecurityStamp = Guid.NewGuid().ToString() | ||
}, | ||
new ApplicationUser | ||
{ | ||
Id = Guid.NewGuid().ToString(), | ||
UserName = "developer", | ||
Email = "developer@gmail.com", | ||
SecurityStamp = Guid.NewGuid().ToString() | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.