public async Task <IActionResult> AddAdminAsync([FromBody] RegisterUserModelDTO RegisterModelDTO) { Guard.Against.NullItem(RegisterModelDTO); //Check for duplicate user-names and e-mail ApplicationUser foundUser = await _userManager.Users.Where(x => x.Email == RegisterModelDTO.UserEmail || x.UserName == RegisterModelDTO.UserName).FirstOrDefaultAsync(); //Throw HTTP 409 Conflict then if (foundUser != null) { return(StatusCode(409, new { message = $"The username / email is already taken and is conflicting with other records, please give an unique username / email." })); } ApplicationUser applicationUser = new ApplicationUser { UserName = RegisterModelDTO.UserName, Email = RegisterModelDTO.UserEmail, FirstName = RegisterModelDTO.FirstName, LastName = RegisterModelDTO.LastName, PhoneNumber = RegisterModelDTO.PhoneNumber, EmailConfirmed = false }; //Create the admin now IdentityResult createResult = await _userManager.CreateAsync(applicationUser, RegisterModelDTO.Password); if (!createResult.Succeeded) { return(BadRequest(new { message = createResult.GetIdentityResultErrorMessage() })); } //Add admin to role await _userManager.AddToRoleAsync(applicationUser, UserType.Admin.ToString()); //Generate JWT now string token = await _tokenManager.GenerateJWTAsync(applicationUser); EmailModel emailModel = new EmailModel { EmailTo = RegisterModelDTO.UserEmail, Body = $"<html><body><a href='{_configSettings.URL.VerifyEmailURL}/{token}'>Click here to verify Email</a></body></html>", Subject = "Verify your Email" }; //Send verify email to admin now await _emailManager.SendEmailAsync(emailModel); //Return HTTP 201 now return(StatusCode(201, new { message = $"Successfully added admin {RegisterModelDTO.UserName}." })); }
public async Task <IActionResult> RegisterAsync([FromBody] RegisterUserModelDTO RegisterUserModelDTO) { Guard.Against.NullItem(RegisterUserModelDTO); //Check for duplicate user-names and e-mail ApplicationUser foundUser = await _userManager.Users.Where(x => x.Email == RegisterUserModelDTO.UserEmail || x.UserName == RegisterUserModelDTO.UserName).FirstOrDefaultAsync(); //Throw HTTP 409 Conflict then if (foundUser != null) { return(StatusCode(409, new { message = $"The username / email is already taken and is conflicting with other records, please give an unique username / email." })); } ApplicationUser applicationUser = new ApplicationUser { UserName = RegisterUserModelDTO.UserName, Email = RegisterUserModelDTO.UserEmail, FirstName = RegisterUserModelDTO.FirstName, LastName = RegisterUserModelDTO.LastName, PhoneNumber = RegisterUserModelDTO.PhoneNumber, EmailConfirmed = false }; IdentityResult createResult = await _userManager.CreateAsync(applicationUser, RegisterUserModelDTO.Password); //User creation failed because of some constraints if (!createResult.Succeeded) { return(BadRequest(new { message = createResult.GetIdentityResultErrorMessage() })); } await _signInManager.SignInAsync(applicationUser, false); await _userManager.AddToRoleAsync(applicationUser, UserType.User.ToString()); //Generate JWT now string jwtToken = await _tokenManager.GenerateJWTAsync(applicationUser); //Send verify email now EmailModel emailModel = new EmailModel { EmailTo = RegisterUserModelDTO.UserEmail, Body = $"<html><body><a href='{_configSettings.URL.VerifyEmailURL}/{jwtToken}'>Click here to verify Email</a><br></body></html>", Subject = "Verify your Email" }; await _emailManager.SendEmailAsync(emailModel); ReferralCode referralCode = GenerateReferralCode(applicationUser.UserName, applicationUser.Id); await _referralCodeService.AddReferralCode(referralCode); //Send Referral code mail now emailModel.Body = $"<html><body><fieldset><legend> Referral code for User - {applicationUser.UserName} </legend> {referralCode.RefCode} </fieldset></body></html>"; emailModel.Subject = $"Visneto Referral Code for new user - {applicationUser.UserName}"; await _emailManager.SendEmailAsync(emailModel); //Return HTTP 201 Created for new user return(StatusCode(201, new { role = UserType.User.ToString(), access_token = jwtToken, expires = 3600, email = string.Empty, user_name = RegisterUserModelDTO.UserName })); }