public async Task <PatientModel> RegisterPatient(PatientModel model) { try { var emailExists = await _userManager.FindByNameAsync(model.Username); if (emailExists != null) { throw new ValidationException("Email Already exists."); } var userDetails = new User() { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, EmailConfirmed = true, IsDeleted = false, EmailNotification = true, SmsNotification = true }; var result = await _userManager.CreateAsync(userDetails, "PatientLogin123!"); if (!result.Succeeded) { throw new ValidationException("Registration failed."); } await _userManager.AddToRoleAsync(userDetails, "patient"); var patient = model.MapTo <Patient>(); patient.UserId = userDetails.Id; _dbContext.Patients.Add(patient); await _dbContext.SaveChangesAsync(); var jwt = await _jwtTokenGenerator.GenerateToken(userDetails); model.Token = jwt; return(model); } catch (Exception ex) { return(null); } }