public async Task <IActionResult> Register(ViewModels.RegisterModel model) { if (ModelState.IsValid) { User user = await db.User.FirstOrDefaultAsync(u => u.Login == model.Login); if (user == null) { user = new User { Name = model.Name, Login = model.Login, Number = model.Number, Password = model.Password }; Role userRole = await db.Role.FirstOrDefaultAsync(r => r.Name == "user"); if (userRole != null) { user.Role = userRole; } db.User.Add(user); await db.SaveChangesAsync(); await Authenticate(user); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", "Некоректні логін і(або) пароль"); } } return(View(model)); }
public ActionResult Register(ViewModels.RegisterModel register) { if (ModelState.IsValid) { User user = db.Users.FirstOrDefault(a => a.Email == register.Email); if (user != null) { ViewBag.Message = "Email already exists!"; return(View()); } user = new User { Name = register.Name, Phone = register.Phone, Password = register.Password, Email = register.Email }; if (register.Latitude != null && register.Longitude != null) { user.Location = new Location { Latitude = register.Latitude, Longitude = register.Longitude }; } db.Users.Add(user); db.SaveChanges(); user = db.Users.FirstOrDefault(a => a.Email == register.Email && a.Password == register.Password); HttpContext.Session["user"] = user; EmailSender.SendEmail(register.Email, "Welcome to Calgary chat", $"Hi {register.Name},\n" + "Welcome to Calgary chat,\n " + "We hope you have a good time with all the different events, feel free to hop into any of the chatrooms to begin the conversion with your peers.\n\n" + "Regards,\n" + "Calgary Events Team"); return(RedirectToAction("ChooseChat", "Home")); } return(View()); }