public Task <Response <LoginCommandView> > Handle(LoginCommand request, CancellationToken cancellationToken) { request.Validate(); if (!request.Invalid) { return(Task.FromResult(new Response <LoginCommandView>(false, request.Notifications, null))); } var user = _repository .AsQueryable() .AsNoTracking() .Include(x => x.Client) .FirstOrDefault(LoginQuery.GetByEmailAndPassword(request.Email, new Password(request.Password))); if (user == null) { request.AddNotification("User", Labels.RegisterNotFound()); return(Task.FromResult(new Response <LoginCommandView>(false, request.Notifications, null))); } var token = _tokenService.CreateToken(user.Client, user); return(Task.FromResult( new Response <LoginCommandView>( true, null, new LoginCommandView(token, user.Name, user.Client.Name) ) ));; }
public async Task <LoginCommand> GrantAuthorizationAsync(LoginCommand command) { var result = await Context .FindAsync(u => u.Username == command.Username); if (result != null && PasswordService.CheckPassword(command.Password, result.Password)) { command.Token = new JwtSecurityTokenHandler().WriteToken( CreateToken(result.Id, result.Username, result.PermissionLevel) ); } else { command.AddNotification(WeatherAttackNotifications.Command.InvalidCredentials); } return(command); }