public async Task <UserResponseDto> Register(RegisterRequestDto dto) { await this.UserExists(dto.Username); this.ValidatePassword(dto.Password); var passwordResult = PasswordEncrypt.ComputeHash(dto.Password); var user = new User { Username = dto.Username, FirstName = dto.FirstName, LastName = dto.LastName, Role = Role.User, Password = passwordResult.Hash, PasswordSalt = passwordResult.Salt }; this.dbContext.Users.Add(user); await this.dbContext.SaveChangesAsync(); return(new UserResponseDto { Id = user.Id, Username = user.Username, Token = this.tokenService.CreateToken(user), Role = user.Role, FirstName = user.FirstName, LastName = user.LastName }); }
public async Task <UserResponseDto> Login(LoginRequestDto dto) { var user = await this.dbContext .Users .SingleOrDefaultAsync(u => u.Username == dto.Username); if (user == null) { throw new InvalidOperationException("Invalid username"); } var passwordHash = PasswordEncrypt.ComputeHash(dto.Password, user.PasswordSalt); if (user.Password != passwordHash) { throw new InvalidOperationException("Invalid password"); } return(new UserResponseDto { Id = user.Id, Username = user.Username, Token = tokenService.CreateToken(user), Role = user.Role, FirstName = user.FirstName, LastName = user.LastName }); }