public async Task <AuthenticateUserResult> RegisterUser(RegisterUserRequest request) { var result = await mediator.Send(new CreateUser { Email = request.Email, DisplayName = request.DisplayName, Password = request.Password }); if (!result.IsSuccessful) { return(new AuthenticateUserResult(result.Error)); } var token = tokenFactory.GenerateEncodedToken(result.User.Code); return(new AuthenticateUserResult { Token = token, Email = result.User.Email, Name = result.User.DisplayName, Code = result.User.Code, ImageUrl = result.User.ImageUrl, }); }
public async Task <MessageResponse <string> > LoginUserAsync(LoginUserRequest message) { var response = new MessageResponse <string>(); if (string.IsNullOrEmpty(message.Username) || !userValidator.ValidateUsername(message.Username)) { response.Errors.Add(errorListProvider.GetError(ErrorCode.IE0007)); return(response); } if (string.IsNullOrEmpty(message.Password)) { response.Errors.Add(errorListProvider.GetError(ErrorCode.IE0008)); return(response); } var user = await userRepository.FindByName(message.Username); if (user == null) { response.Errors.Add(errorListProvider.GetError(ErrorCode.IE0005)); return(response); } if (await userRepository.CheckPassword(user, message.Password)) { response.Message = await jwtFactory.GenerateEncodedToken(user.Id, user.UserName); } else { response.Errors.Add(errorListProvider.GetError(ErrorCode.IE0004)); } return(response); }
public static async Task <string> GenerateJwt(ClaimsIdentity identity, IJwtTokenFactory jwtFactory, string userName, JwtIssuerOptions jwtOptions, JsonSerializerSettings serializerSettings) { var response = new { id = identity.Claims.Single(c => c.Type == "id").Value, auth_token = await Task.FromResult(jwtFactory.GenerateEncodedToken(userName, identity)), expires_in = (int)TimeSpan.FromMinutes(1).TotalSeconds }; return(JsonConvert.SerializeObject(response, serializerSettings)); }