I finished! On to the next chapter

Outline

Demo: Implement Login

In Conduit.Repositories Project

Modify AccountRepository.LoginAsync Method
  • Add an account variable by calling the Context object
  • From the Accounts get the record(s) where it matches the user.Email
  • Include the Person object in the expression
  • Make it await
  • ConfigureAwait set to false
Return LoginFailedException if the following conditions are true
  • The account variable is null
  • VerifyPassword fails to validate the password correctly
Create the user object
  • Reuse the method CreateUser
  • Reuse the method CreateToken
  • Return the user

This is the code for the LoginAsync method:

        public async Task<User> LoginAsync(Login login)
        {
            Account account = await Context
                                        .Accounts
                                        .Where(x => x.Email == login.Email)
                                        .Include(p => p.Person)
                                        .FirstOrDefaultAsync().ConfigureAwait(false);
            if (account is null)
            {
                throw new LoginFailedException("Login Failed - email not found.");
            }
            if (!VerifyPassword(login.Password, account.PasswordHash, account.PasswordSalt))
            {
                throw new LoginFailedException("Login Failed - password is incorrect.");
            }
            User user = CreateUser(account);
            user.Token = CreateToken(user);
            return user;
        }