public async Task <IActionResult> Register(RegisterViewModel registerVM, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { _logger.LogInformation("********Role: " + registerVM.Role + "*************"); IdentityResult result = null; Doctor doctor = null; Patient patient = null; //var user = new ApplicationUser { UserName = registerVM.Email, Email = registerVM.Email }; if (registerVM.Role == Roles.DOCTOR) { doctor = registerVM.GetNewDoctor(); // new Doctor { // UserName = registerVM.Email, // Email = registerVM.Email, // DegreeAbbreviation = registerVM.DegreeAbbreviation //}; result = await _userManager.CreateAsync(doctor, registerVM.Password); } else if (registerVM.Role == Roles.PATIENT) { patient = await registerVM.GetNewPatient(_doctorRepository); // new Patient { // UserName = registerVM.Email, // Email = registerVM.Email, // Doctor = await _doctorRepository.ReadAsync( registerVM.DoctorUserName ), // DoctorUserName = registerVM.DoctorUserName //}; result = await _userManager.CreateAsync(patient, registerVM.Password); } if (result != null && result.Succeeded) { var user = registerVM.Role == Roles.DOCTOR ? (ApplicationUser)doctor : (ApplicationUser)patient; await _userManager.AddToRoleAsync(user, registerVM.Role.ToString().ToUpper()); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(registerVM.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToLocal(returnUrl)); } AddErrors(result); //if() } // If we got this far, something failed, redisplay form return(View(registerVM)); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> Register(RegisterViewModel model) { var doctor = await _doctorRepository.ReadAsync(model.DoctorUserName); Patient patient = await model.GetNewPatient(_doctorRepository); patient.Doctor = doctor; patient.DoctorUserName = doctor.UserName; var result = await _userManager.CreateAsync(patient, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); patient.CreatedAt = DateTime.Now; patient.UpdatedAt = patient.CreatedAt; //patient = await _patientRepository.ReadAsync( model.Email ); //patient.Doctor = doctor; //patient.DoctorUserName = model.DoctorUserName; await _patientRepository.UpdateAsync(model.Email, patient); var code = await _userManager.GenerateEmailConfirmationTokenAsync(patient); var callbackUrl = Url.EmailConfirmationLink(patient.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); //await _signInManager.SignInAsync( patient, isPersistent: false ); _logger.LogInformation("User created a new account with password."); return(await Login(model.Email, model.Password)); // new JsonResult( new { } ); // } // Else, if registering didn't succeed... patient = await _patientRepository.ReadAsync(model.Email); if (patient != null && !String.IsNullOrEmpty(patient.RemoteLoginToken.ToString())) { return(new JsonResult(new { success = true, errorCode = ErrorCode.USER_ALREADY_LOGGED_IN, patient.RemoteLoginToken, patient.RemoteLoginExpiration, patient.DoctorUserName, patient.Email, patient.UserName, doctorId = patient.Doctor != null ? patient.Doctor.Id : "" })); } return(await Login(model.Email, model.Password)); //new JsonResult( new { success = false, errorCode = ErrorCode.USER_ALREADY_REGISTERED } ); }
public async Task <IActionResult> Register(RegisterViewModel registerVM, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { _logger.LogInformation("********Role: " + registerVM.Role + "*************"); //IdentityResult result = null; //var user = registerVM.Role == Roles.DOCTOR ? (ApplicationUser) doctor : (ApplicationUser) patient; switch (registerVM.Role) { case Roles.DOCTOR: Doctor doctor = registerVM.GetNewDoctor(); doctor = await _doctorRepository.CreateAsync(doctor); await _userManager.AddToRoleAsync(doctor, Roles.DOCTOR.ToUpper()); //await _users.AssignRole(doctor.UserName, Roles.DOCTOR.ToUpper()); await SetupUser(doctor, registerVM); break; case Roles.PATIENT: Patient patient = await registerVM.GetNewPatient(_doctorRepository); patient = await _patientRepository.CreateAsync(patient); await _userManager.AddToRoleAsync(patient, Roles.PATIENT); await SetupUser(patient, registerVM); break; case Roles.DEVELOPER: Developer dev = registerVM.GetNewDeveloper(); //var result = await _userManager.CreateAsync(dev, registerVM.Password); dev = await _developerRepository.CreateAsync(dev); await _userManager.AddToRoleAsync(dev, Roles.DEVELOPER); await SetupUser(dev, registerVM); break; default: break; } //var user = new ApplicationUser { UserName = registerVM.Email, Email = registerVM.Email }; //if ( result != null && result.Succeeded ) //{ return(RedirectToLocal(returnUrl)); //} // if //AddErrors( result ); } // if // If we got this far, something failed, redisplay form return(View(registerVM)); } // Register