public async Task <UcLoginResponse> Handle(UcLoginRequest message) { var valid = !string.IsNullOrWhiteSpace(message.UserName) && !string.IsNullOrWhiteSpace(message.Password); if (valid) { var user = await this.userRepository.FindByName(message.UserName); if (user != null) { var passwordValid = await this.userRepository.CheckPassword(user, message.Password); if (passwordValid) { var refreshToken = this.tokenFactory.GenerateToken(); user.AddRefreshToken(refreshToken, user.Id); await this.userRepository.Update(user); var accessToken = await this.jwtFactory.GenerateEncodedToken(user.IdentityId, user.UserName); return(new UcLoginResponse(accessToken, refreshToken, true)); } } } var error = new Error("login_failure", "Invalid username or password."); return(new UcLoginResponse(new[] { error })); }
public async Task <ActionResult> Login([FromBody] LoginRequest request) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var useCaseRequest = new UcLoginRequest(request.Username, request.Password); var result = await this.loginUseCase.Handle(useCaseRequest); result.UserName = request.Username; var contentResult = new JsonContentResult { StatusCode = (int)(result.Success ? HttpStatusCode.OK : HttpStatusCode.Unauthorized), Content = result.Success ? JsonSerializer.SerializeObject(new TokensResponse(result.AccessToken, result.RefreshToken, result.UserName)) : JsonSerializer.SerializeObject(result.Errors) }; return(contentResult); }