public async Task <ApiResponse <RegisterResultApiModel> > RegisterAsync( [FromBody] RegisterCredentialsApiModel registerCredentials) { // TODO: Localize all strings // The message when we fail to login var invalidErrorMessage = "Prosze wypełnić wszystkie pola do zarejestrowania się"; var invalidConfirmPassword = "******"; // The error response for a failed login var errorResponse = new ApiResponse <RegisterResultApiModel> { // Set error message ErrorMessage = invalidErrorMessage }; var errorResponse2 = new ApiResponse <RegisterResultApiModel> { // Set error message ErrorMessage = invalidConfirmPassword }; // If we have no credentials... if (registerCredentials == null) { // Return failed response return(errorResponse); } // Make sure we have a user name if (string.IsNullOrWhiteSpace(registerCredentials.Username) || string.IsNullOrWhiteSpace(registerCredentials.FirstName) || string.IsNullOrWhiteSpace(registerCredentials.LastName) || string.IsNullOrWhiteSpace(registerCredentials.Password) || string.IsNullOrWhiteSpace(registerCredentials.Email)) { // Return error message to user return(errorResponse); } if (registerCredentials.Password != registerCredentials.ConfirmPassword) { return(errorResponse2); } SendMail(registerCredentials.Email, registerCredentials.FirstName); // Create the desired user from the given details var user = new ApplicationUser { UserName = registerCredentials.Username, FirstName = registerCredentials.FirstName, LastName = registerCredentials.LastName, Email = registerCredentials.Email }; // Try and create a user var result = await mUserManager.CreateAsync(user, registerCredentials.Password); //var userRoles = await mUserManager.GetRolesAsync(user); // If the registration was successful... if (result.Succeeded) { // Get the user details var userIdentity = await mUserManager.FindByNameAsync(user.UserName); await mUserManager.AddToRoleAsync(userIdentity, "User"); // Send email verification //await SendUserEmailVerificationAsync(user); var roles = await mUserManager.GetRolesAsync(user); string role = ""; foreach (var r in roles) { role = r; } // Return valid response containing all users details return(new ApiResponse <RegisterResultApiModel> { Response = new RegisterResultApiModel { Id = userIdentity.Id, FirstName = userIdentity.FirstName, LastName = userIdentity.LastName, Email = userIdentity.Email, Username = userIdentity.UserName, Token = userIdentity.GenerateJwtToken(role), } }); } // Otherwise if it failed... else { // Return the failed response return(new ApiResponse <RegisterResultApiModel> { // Aggregate all errors into a single error string ErrorMessage = result.Errors.AggregateErrors() }); } }
public async Task <ApiResponse <RegisterResultApiModel> > RegisterAsync([FromBody] RegisterCredentialsApiModel registerCredentials) { // TODO: Localize all strings // The message when we fail to login var invalidErrorMessage = "Please provide all required details to register for an account"; // The error response for a failed login var errorResponse = new ApiResponse <RegisterResultApiModel> { // Set error message ErrorMessage = invalidErrorMessage }; // If we have no credentials... if (registerCredentials == null) { // Return failed response return(errorResponse); } // Make sure we have a user name if (string.IsNullOrWhiteSpace(registerCredentials.Username)) { // Return error message to user return(errorResponse); } // Create the desired user from the given details var user = new ApplicationUser { UserName = registerCredentials.Username, FirstName = registerCredentials.FirstName, LastName = registerCredentials.LastName, Email = registerCredentials.Email }; // Try and create a user var result = await mUserManager.CreateAsync(user, registerCredentials.Password); // If the registration was successful... if (result.Succeeded) { // Get the user details var userIdentity = await mUserManager.FindByNameAsync(user.UserName); // Send email verification await SendUserEmailVerificationAsync(user); // Return valid response containing all users details return(new ApiResponse <RegisterResultApiModel> { Response = new RegisterResultApiModel { FirstName = userIdentity.FirstName, LastName = userIdentity.LastName, Email = userIdentity.Email, Username = userIdentity.UserName, Token = userIdentity.GenerateJwtToken() } }); } // Otherwise if it failed... else { // Return the failed response return new ApiResponse <RegisterResultApiModel> { // Aggregate all errors into a single error string ErrorMessage = result.Errors.AggregateErrors() } }; }
public async Task <ApiResponse <RegisterResultApiModel> > PersonalDataAsync( [FromBody] RegisterCredentialsApiModel registerCredentials) { // TODO: Localize all strings // The message when we fail to login var invalidErrorMessage = "Prosze wypełnić wszystkie pola do zarejestrowania się"; // The error response for a failed login var errorResponse = new ApiResponse <RegisterResultApiModel> { // Set error message ErrorMessage = invalidErrorMessage }; var exist = await mUserManager.FindByEmailAsync(registerCredentials.Email); if (exist != null) { exist.LastName = registerCredentials.LastName; exist.FirstName = registerCredentials.FirstName; await mUserManager.UpdateAsync(exist); return(new ApiResponse <RegisterResultApiModel> { ErrorMessage = "Sukces" }); } else { // If we have no credentials... if (registerCredentials == null) { // Return failed response return(errorResponse); } // Make sure we have a user name if (string.IsNullOrWhiteSpace(registerCredentials.FirstName) || string.IsNullOrWhiteSpace(registerCredentials.LastName) || string.IsNullOrWhiteSpace(registerCredentials.Email)) { // Return error message to user return(errorResponse); } // Create the desired user from the given details var user = new ApplicationUser { FirstName = registerCredentials.FirstName, LastName = registerCredentials.LastName, Email = registerCredentials.Email }; user.UserName = user.Id; // Try and create a user var result = await mUserManager.CreateAsync(user, "Password"); // If the registration was successful... if (result.Succeeded) { // Get the user details var userIdentity = await mUserManager.FindByNameAsync(user.UserName); // Send email verification //await SendUserEmailVerificationAsync(user); // Return valid response containing all users details return(new ApiResponse <RegisterResultApiModel> { ErrorMessage = "Sukces" }); } // Otherwise if it failed... else { // Return the failed response return new ApiResponse <RegisterResultApiModel> { // Aggregate all errors into a single error string ErrorMessage = result.Errors.AggregateErrors() } }; } }
public async Task <ActionResult <ApiResponse <RegisterCredentialsResultApiModel> > > Register([FromBody] RegisterCredentialsApiModel userCredentials) { var user = new ApplicationUser { Email = userCredentials?.Email, UserName = userCredentials.Email }; var result = await _userManager.CreateAsync(user, userCredentials.Password); if (result.Succeeded) { // Get the user details var userIdentity = await _userManager.FindByEmailAsync(user.Email); // Send verification code var emailVerificationCode = await _userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationUrl = $"http://{Request.Host.Value}/api/verify/email/?userId={HttpUtility.UrlEncode(userIdentity.Id)}&emailToken={HttpUtility.UrlEncode(emailVerificationCode)}"; // Send email // Read token parameters from the configuration file var jwtParameters = new JwtParameters { SecretKey = _configuration["Jwt:Key"], Audience = _configuration["Jwt:Audience"], Issuer = _configuration["Jwt:Issuer"] }; return(Ok(new ApiResponse <RegisterCredentialsResultApiModel> { Response = new RegisterCredentialsResultApiModel { FirstName = userIdentity.FirstName, LastName = userIdentity.LastName, Email = userIdentity.Email, Token = userIdentity.GenerateJwtToken(jwtParameters) } })); } else { return(BadRequest(new ApiResponse <RegisterCredentialsResultApiModel> { Errors = result.Errors.ParseErrors() })); } }
public async Task <ApiResponse <RegisterResultApiModel> > RegisterAsync([FromBody] RegisterCredentialsApiModel registerCredentials) { var errorResponse = new ApiResponse <RegisterResultApiModel> { ErrorMessage = UserMessages.UserNotFound }; //TODO: Allow registration of username only var userId = GenerateNewGuid(); // Create the desired user from the given details var user = new ApplicationUser { UserName = registerCredentials.Username, Email = registerCredentials.Email, PhoneNumber = registerCredentials.PhoneNumber, Employee = new Employee { Id = userId, Gender = (Gender)registerCredentials.Gender, FirstName = registerCredentials.FirstName, LastName = registerCredentials.LastName, CreatedBy = userId, ModifiedBy = userId, } }; //Check if email is unique var isNotUnique = await mContext.Users.Where(x => x.NormalizedEmail == registerCredentials.Email.NormalizedUpper()).Select(x => true).SingleOrDefaultAsync(); if (isNotUnique) { errorResponse.ErrorMessage = UserMessages.EmailHasBeenTakenAlready; return(errorResponse); } using var transaction = await mContext.Database.BeginTransactionAsync(); try { // Try and create a user var result = await mUserManager.CreateAsync(user, registerCredentials.Password); // If the registration was successful... if (result.Succeeded) { // Get the user details var userIdentity = await mUserManager.Users.Include(x => x.Employee).Select(x => x).FirstOrDefaultAsync(x => x.UserName == registerCredentials.Username); //Add Employee Role to the user await mUserManager.AddToRoleAsync(userIdentity, AspNetRolesDefaults.Employee); //Send email verification //await SendUserEmailVerificationAsync(userIdentity); //Commit the whole transaction await transaction.CommitAsync(); // Return valid response containing necessary users details return(new ApiResponse <RegisterResultApiModel> { Response = new RegisterResultApiModel { FirstName = userIdentity.Employee.FirstName, LastName = userIdentity.Employee.LastName, Token = userIdentity.GenerateJwtToken(), } }); } // Otherwise if it failed... else { throw new Exception(result.Errors.AggregateErrors()); } } catch (Exception ex) { //Rollback the whole transaction await transaction.RollbackAsync(); // Return the failed response return(new ApiResponse <RegisterResultApiModel> { ErrorMessage = ex.Message }); }; }
public async Task <ApiResponse <RegisterResultApiModel> > RegisterAsync([FromBody] RegisterCredentialsApiModel registerCredentials) { // TODO: Localize all strings // The message when we fail to login var invalidErrorMessage = "Please provide all required details to register for an account"; // The error response for a failed login var errorResponse = new ApiResponse <RegisterResultApiModel> { // Set error message ErrorMessage = invalidErrorMessage }; // If we have no credentials... if (registerCredentials == null) { // Return failed response return(errorResponse); } // Make sure we have a user name if (string.IsNullOrWhiteSpace(registerCredentials.Username)) { // Return error message to user return(errorResponse); } // Create the desired user from the given details var user = new ApplicationUser { UserName = registerCredentials.Username, FirstName = registerCredentials.FirstName, LastName = registerCredentials.LastName, Email = registerCredentials.Email }; // Try and create a user var result = await mUserManager.CreateAsync(user, registerCredentials.Password); // If the registration was successful... if (result.Succeeded) { // Get the user details var userIdentity = await mUserManager.FindByNameAsync(registerCredentials.Username); // Generate an email verification code var emailVerificationCode = await mUserManager.GenerateEmailConfirmationTokenAsync(user); // TODO: Replace with APIRoutes that will contain the static routes to use var confirmationUrl = $"http://{Request.Host.Value}/api/verify/email/{HttpUtility.UrlEncode(userIdentity.Id)}/{HttpUtility.UrlEncode(emailVerificationCode)}"; // Email the user the verification code await FasettoEmailSender.SendUserVerificationEmailAsync(user.UserName, userIdentity.Email, confirmationUrl); // Return valid response containing all users details return(new ApiResponse <RegisterResultApiModel> { Response = new RegisterResultApiModel { FirstName = userIdentity.FirstName, LastName = userIdentity.LastName, Email = userIdentity.Email, Username = userIdentity.UserName, Token = userIdentity.GenerateJwtToken() } }); } // Otherwise if it failed... else { // Return the failed response return new ApiResponse <RegisterResultApiModel> { // Aggregate all errors into a single error string ErrorMessage = result.Errors?.ToList() .Select(f => f.Description) .Aggregate((a, b) => $"{a}{Environment.NewLine}{b}") } }; }
public async Task <ApiResponse <RegisterResultApiModel> > RegisterAsync([FromBody] RegisterCredentialsApiModel registerCredentials) { var invalidErrorMessage = "Please provide all required details to register for an account"; var errorResponse = new ApiResponse <RegisterResultApiModel> { ErrorMessage = invalidErrorMessage }; if (registerCredentials == null) { return(errorResponse); } if (string.IsNullOrWhiteSpace(registerCredentials.Username)) { return(errorResponse); } var user = new ApplicationUser { UserName = registerCredentials.Username, FirstName = registerCredentials.FirstName, LastName = registerCredentials.LastName, Email = registerCredentials.Email }; // Try and create a user var result = await mUserManager.CreateAsync(user, registerCredentials.Password); if (result.Succeeded) { var userIdentity = await mUserManager.FindByNameAsync(registerCredentials.Username); var emailVerificationCode = await mUserManager.GenerateEmailConfirmationTokenAsync(user); var confirmationUrl = $"http://{Request.Host.Value}/api/verify/email/{HttpUtility.UrlEncode(userIdentity.Id)}/{HttpUtility.UrlEncode(emailVerificationCode)}"; await MessengerEmailSender.SendUserVerificationEmailAsync(user.UserName, userIdentity.Email, confirmationUrl); return(new ApiResponse <RegisterResultApiModel> { Response = new RegisterResultApiModel { FirstName = userIdentity.FirstName, LastName = userIdentity.LastName, Email = userIdentity.Email, Username = userIdentity.UserName, Token = userIdentity.GenerateJwtToken() } }); } else { return new ApiResponse <RegisterResultApiModel> { ErrorMessage = result.Errors?.ToList() .Select(f => f.Description) .Aggregate((a, b) => $"{a}{Environment.NewLine}{b}") } }; }
public async Task <ApiResponse <RegisterResultApiModel> > RegisterAsync([FromBody] RegisterCredentialsApiModel registerCredentials) { // TODO: Localize all strings // The message when we failed to login var invalidErrorMessage = "Please provide all required details to register for an account"; // The error response for a failed login var errorResponse = new ApiResponse <RegisterResultApiModel> { // Set error message ErrorMessage = invalidErrorMessage, }; // Make sure we have a register credentials if (registerCredentials == null) { // Return error message to user return(errorResponse); } // Make sure we have a user name if (string.IsNullOrWhiteSpace(registerCredentials.Username)) { // Return error message to user return(errorResponse); } // Create the desired user from the given details var user = new ApplicationUser { UserName = registerCredentials?.Username, FirstName = registerCredentials?.FirstName, LastName = registerCredentials?.LastName, Email = registerCredentials?.Email, }; // Try and create a user var result = await mUserManager.CreateAsync(user, registerCredentials.Password); // If the registration was successful... if (result.Succeeded) { // Get the user details var userIdentity = await mUserManager.FindByNameAsync(registerCredentials.Username); // Generate an email verification code var emailVerificationCode = await mUserManager.GenerateEmailConfirmationTokenAsync(user); // TODO: Email the user the verfication code // Returns valid response containing all users details return(new ApiResponse <RegisterResultApiModel> { Response = new RegisterResultApiModel { FirstName = userIdentity.FirstName, LastName = userIdentity.LastName, Email = userIdentity.Email, Username = userIdentity.UserName, Token = userIdentity.GenerateJwtToken(), }, }); } // Otherwise if it failed... else { // Return the failed response return new ApiResponse <RegisterResultApiModel> { // Aggregate all errors into a single error string ErrorMessage = result.Errors?.ToList() .Select(f => f.Description) .Aggregate((a, b) => $"{a}{Environment.NewLine}{b}"), } }; }