Skip to content

Commit

Permalink
Feat: Added the IUserRepo and InMem implementation, refactered some u…
Browse files Browse the repository at this point in the history
…ser stuff in the JWT Generator, finished episode 3 of the DDD REST API with .NET
Legolas2222 committed Oct 2, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 616bce7 commit 0888e25
Showing 12 changed files with 117 additions and 77 deletions.
16 changes: 8 additions & 8 deletions TodoistClone.Api/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
@@ -25,10 +25,10 @@ public IActionResult Register(RegisterRequest request)
request.Password);

var response = new AuthenticationResponse(
authResult.Id,
authResult.FirstName,
authResult.LastName,
authResult.Email,
authResult.user.Id,
authResult.user.Email,
authResult.user.Email,
authResult.user.Email,
authResult.Token);

return Ok(response);
@@ -42,10 +42,10 @@ public IActionResult Login(LoginRequest request)
request.Password);

var response = new AuthenticationResponse(
authResult.Id,
authResult.FirstName,
authResult.LastName,
authResult.Email,
authResult.user.Id,
authResult.user.Email,
authResult.user.Email,
authResult.user.Email,
authResult.Token);

return Ok(response);
32 changes: 0 additions & 32 deletions TodoistClone.Api/Controllers/WeatherForecastController.cs

This file was deleted.

2 changes: 1 addition & 1 deletion TodoistClone.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
*/
var app = builder.Build();

app.UseHttpsRedirection();
//app.UseHttpsRedirection();

app.UseAuthorization();

12 changes: 0 additions & 12 deletions TodoistClone.Api/WeatherForecast.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -3,11 +3,12 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoistClone.Domain.Entities;

namespace TodoistClone.Application.Common.Interfaces.Authentication
{
public interface IJwtTokenGenerator
{
string GenerateToken(Guid userId, string firstName, string lastName);
string GenerateToken(User user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoistClone.Domain.Entities;

namespace TodoistClone.Application.Common.Interfaces.Persistence
{
public interface IUserRepository
{
void Add(User user);
User? GetUserByEmail(string email);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace TodoistClone.Application.Services.Authentication
using TodoistClone.Domain.Entities;

namespace TodoistClone.Application.Services.Authentication
{
public record AuthenticationResult(
Guid Id,
string FirstName,
string LastName,
string Email,
User user,
string Token);
}
Original file line number Diff line number Diff line change
@@ -4,44 +4,68 @@
using System.Text;
using System.Threading.Tasks;
using TodoistClone.Application.Common.Interfaces.Authentication;
using TodoistClone.Application.Common.Interfaces.Persistence;
using TodoistClone.Domain.Entities;

namespace TodoistClone.Application.Services.Authentication
{
public class AuthenticationService : IAuthenticationService
{
private IJwtTokenGenerator _jwtTokenGenerator;
private IUserRepository _userRepository;

public AuthenticationService(IJwtTokenGenerator jwtTokenGenerator)
public AuthenticationService(IJwtTokenGenerator jwtTokenGenerator, IUserRepository userRepository)
{
_jwtTokenGenerator = jwtTokenGenerator;
_userRepository = userRepository;
}

public AuthenticationResult Register(string firstName, string lastName, string email, string password)
{
// Check if user already exists
// Check that the user doesn't already exist
if (_userRepository.GetUserByEmail(email) is not null)
{
throw new Exception("User with given email already exists");
}

// Create user (generate unique ID)

// Create user (generate unique ID) & persist that user to the DB
var user = new User
{
Email = email,
Password = password,
FirstName = firstName,
LastName = lastName
};
_userRepository.Add(user);

// Generate JWT token
Guid userId = Guid.NewGuid();
var token = _jwtTokenGenerator.GenerateToken(userId, firstName, lastName);
var token = _jwtTokenGenerator.GenerateToken(user);

return new AuthenticationResult(
userId,
firstName,
lastName,
email,
user,
token);
}

public AuthenticationResult Login(string email, string password)
{
// Validate the user exists
if (_userRepository.GetUserByEmail(email) is not User user)
{
throw new Exception("User with given email does not exist");
}
// Check the password
if (user.Password != password)
{
throw new Exception("The given password is not correct");
}

var token = _jwtTokenGenerator.GenerateToken(user);

// Create JWT token and return that token
return new AuthenticationResult(
Guid.NewGuid(),
"firstName",
"lastName",
email,
"token");
user,
token);
}
}
}
17 changes: 17 additions & 0 deletions TodoistClone.Domain/Entities/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TodoistClone.Domain.Entities
{
public class User
{
public Guid Id { get; set; } = Guid.NewGuid();
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
using System.Threading.Tasks;
using TodoistClone.Application.Common.Interfaces.Authentication;
using TodoistClone.Application.Common.Interfaces.Services;
using TodoistClone.Domain.Entities;

namespace TodoistClone.Infrastructure.Authentication
{
@@ -20,17 +21,17 @@ public JwtTokenGenerator(IDateTimeProvider dateTimeProvider)
_dateTimeProvider = dateTimeProvider;
}

public string GenerateToken(Guid userId, string firstName, string lastName)
public string GenerateToken(User user)
{
var signingCredentials = new SigningCredentials(
new SymmetricSecurityKey(Encoding.UTF8.GetBytes("extremely-secret-key-that-no-one-knows")),
SecurityAlgorithms.HmacSha256);

var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, userId.ToString()),
new Claim(JwtRegisteredClaimNames.GivenName, firstName),
new Claim(JwtRegisteredClaimNames.FamilyName, lastName),
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new Claim(JwtRegisteredClaimNames.GivenName, user.FirstName),
new Claim(JwtRegisteredClaimNames.FamilyName, user.LastName),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};

3 changes: 3 additions & 0 deletions TodoistClone.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using TodoistClone.Application.Common.Interfaces.Authentication;
using TodoistClone.Application.Common.Interfaces.Persistence;
using TodoistClone.Application.Common.Interfaces.Services;
using TodoistClone.Infrastructure.Authentication;
using TodoistClone.Infrastructure.Persistence;
using TodoistClone.Infrastructure.Services;

namespace TodoistClone.Infrastructure
@@ -13,6 +15,7 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
{
services.AddSingleton<IJwtTokenGenerator, JwtTokenGenerator>();
services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
services.AddScoped<IUserRepository, UserRepositoryInMemory>();
return services;
}

24 changes: 24 additions & 0 deletions TodoistClone.Infrastructure/Persistence/UserRepositoryInMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoistClone.Application.Common.Interfaces.Persistence;
using TodoistClone.Domain.Entities;

namespace TodoistClone.Infrastructure.Persistence
{
public class UserRepositoryInMemory : IUserRepository
{
private static List<User> _users = new List<User>();
public void Add(User user)
{
_users.Add(user);
}

public User? GetUserByEmail(string email)
{
return _users.SingleOrDefault(x => x.Email == email);
}
}
}

0 comments on commit 0888e25

Please sign in to comment.