Outline

Solution: Implement Interface

In the Conduit.Repository Project

Modify AccountRepository.cs by Implementing the IAccountRepository Interface
  • Hover over the error on IAccountRepository
  • Select [Show Potential Fixes]
  • Select [Implement interface]

Modify the GetCurrentuserAsync Method by adding the following code

Account account = await GetLoggedInUser(); // We'll implement this in the next demo
if (account is null)
{
   throw new InvalidCredentialsException("Invalid user - Please login using a valid email & password");
}
return CreateUser(account); // We get to reuse an existing method. Yay!!!

Here's the code for the AccountRepository.cs file (existing code omitted for brevity):

using ...

namespace Conduit.Repositories
{
    public class AccountRepository : IAccountRepository
    {
        private IHttpContextAccessor HttpContextAccessor;
        private ConduitContext Context;
        private IMapper Mapper;
        private IConfiguration Configuration;
        private ILogger<AccountRepository> Logger;

        private async Task<Account> CreateAccountAsync(Register register)...

        private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)...

        private async Task<bool> UserExistAsync(Register register)...

        private User CreateUser(Account account)...

        private string CreateToken(User user)...

        private bool VerifyPassword(string password, byte[] passwordHash, byte[] passwordSalt)...

        public async Task<User> RegisterUserAsync(Register register)...

        public async Task<User> LoginAsync(Login login)...

        public async Task<User> GetCurrentUserAsync()
        {
            Account account = await GetLoggedInUser();
            if (account is null)
            {
                throw new InvalidCredentialsException("Invalid user - Please login using a valid email & password.");
            }
            return CreateUser(account);
        }

        public async Task<Account> GetLoggedInUser()
        {
            throw new NotImplementedException();
        }

        public AccountRepository(IHttpContextAccessor httpContextAccessor, ConduitContext context, IMapper mapper, IConfiguration configuration, ILogger<AccountRepository> logger)...
    }
}
 

I finished! On to the next chapter