public async Task <IActionResult> Login(LoginVM model) { if (!ModelState.IsValid) { return(View(model)); } User user = await _authenticateService.LoginAsync(model.EmailAddress, model.Password, model.RememberMe); if (user != null) { return(this.RedirectToHomePage()); } else { ViewData["ErrorMessage"] = "Incorrect email address or password."; return(View(model)); } }
/// <summary> /// Gets the id of the current authenticated user, or logins/registers the current user by the specified parameters. /// </summary> /// <remarks>This modifies the ViewData also, if there are errors.</remarks> /// <param name="signMethod">The sign method to use: login/register.</param> /// <param name="loginVM">The login view model of the checkout view model.</param> /// <param name="registerVM">The register view model of the checkout view model.</param> /// <returns>Returns the user id if authentication succeeded, otherwise null.</returns> private async Task <int?> GetOrAuthenticateUser(SignMethod?signMethod, CheckoutVM.ConditionalLoginVM loginVM, CheckoutVM.ConditionalRegisterVM registerVM) { // Checks if the user is already authenticated: if (HttpContext.User.Identity.IsAuthenticated) { return(HttpContext.User.Identity.GetId().Value); } // Otherwise, needs to authenticate the user: else { User user; if (signMethod == SignMethod.Login) { user = await _authenticateService.LoginAsync(loginVM.EmailAddress, loginVM.Password, loginVM.RememberMe); if (user == null) { ViewData["Error_Login"] = "******"; } } else if (signMethod == SignMethod.Register) { user = await _authenticateService.RegisterAsync(registerVM.FirstName, registerVM.LastName, registerVM.EmailAddress, registerVM.Password, registerVM.SubscribeNewsletter); } else { throw new ArgumentOutOfRangeException(nameof(signMethod), $"{nameof(signMethod)} is not defined."); } if (user != null) { return(user.Id); } else { return(null); } } }