public async Task <IActionResult> Register([FromBody] UserRegistrationReqObj regRequest) { try { if (!ModelState.IsValid) { return(BadRequest(new AuthFailedResponse { Errors = ModelState.Values.SelectMany(x => x.Errors.Select(xx => xx.ErrorMessage)) })); } var authResponse = await _identityService.RegisterAsync(regRequest); if (!authResponse.Success) { return(BadRequest(new AuthFailedResponse { Errors = authResponse.Errors })); } return(Ok(new AuthSuccessResponse { Token = authResponse.Token, RefreshToken = authResponse.RefreshToken })); } catch (Exception ex) { throw ex.InnerException; } }
public async Task <AuthenticationResult> RegisterAsync(UserRegistrationReqObj userRegistration) { try { var existingUser = await _userManager.FindByEmailAsync(userRegistration.Email); if (existingUser != null) { return(new AuthenticationResult { Errors = new[] { "User with this email address already exist" } }); } var user = new ApplicationUser { Email = userRegistration.Email, UserName = userRegistration.Email, }; var createdUser = await _userManager.CreateAsync(user, userRegistration.Password); if (!createdUser.Succeeded) { return(new AuthenticationResult { Errors = createdUser.Errors.Select(x => x.Description) }); } return(await GenerateAuthenticationResultForUserAsync(user)); } catch (Exception ex) { #region Log error var errorCode = ErrorID.Generate(4); _logger.Error($"ErrorID : RegisterAsync{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new AuthenticationResult { Status = new APIResponseStatus { Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Please tyr again later", MessageId = errorCode, TechnicalMessage = $"ErrorID : RegisterAsync{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <AuthenticationResult> RegisterAsync(UserRegistrationReqObj userRegistration) { try { var existingUser = await _userManager.FindByEmailAsync(userRegistration.Email); if (existingUser != null) { return(new AuthenticationResult { Errors = new[] { "User with this email address already exist" } }); } var user = new ApplicationUser { Email = userRegistration.Email, UserName = userRegistration.Email, FullName = userRegistration.FullName, NationalIdentificationNumber = userRegistration.NationalIdentificationNumber, PhoneNumber = userRegistration.PhoneNumber }; var createdUser = await _userManager.CreateAsync(user, userRegistration.Password); if (!createdUser.Succeeded) { return(new AuthenticationResult { Errors = createdUser.Errors.Select(x => x.Description) }); } return(await GenerateAuthenticationResultForUserAsync(user)); } catch (Exception ex) { throw ex; } }