public async Task <IActionResult> SignIn(SignInViewModel model)
        {
            if (ModelState.IsValid)
            {
                CountryClubUser existingUser = await this._signInManager.UserManager.FindByNameAsync(model.UserName);

                if (existingUser != null)
                {
                    Microsoft.AspNetCore.Identity.SignInResult passwordResult = await this._signInManager.CheckPasswordSignInAsync(existingUser, model.Password, false);

                    if (passwordResult.Succeeded)
                    {
                        await this._signInManager.SignInAsync(existingUser, false);

                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("PasswordIncorrect", "Username or Password is incorrect.");
                    }
                }
                else
                {
                    ModelState.AddModelError("UserDoesNotExist", "Username or Password is incorrect.");
                }
            }
            return(View());
        }
Example #2
0
        public async Task <IActionResult> Index()
        {
            CheckoutViewModel model = new CheckoutViewModel();

            await GetCurrentCart(model);

            if (User.Identity.IsAuthenticated)
            {
                CountryClubUser currentUser = await _signInManager.UserManager.GetUserAsync(User);

                Braintree.CustomerSearchRequest search = new Braintree.CustomerSearchRequest();
                search.Email.Is(currentUser.Email);
                var searchResult = await _brainTreeGateway.Customer.SearchAsync(search);

                if (searchResult.Ids.Count > 0)
                {
                    Braintree.Customer customer = searchResult.FirstItem;
                    model.CreditCards = customer.CreditCards;
                    model.Addresses   = customer.Addresses;
                }
            }
            if (model.Cart == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            return(View(model));
        }
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                CountryClubUser newUser = new CountryClubUser
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber
                };

                IdentityResult creationResult = await this._signInManager.UserManager.CreateAsync(newUser);

                if (creationResult.Succeeded)
                {
                    IdentityResult passwordResult = await this._signInManager.UserManager.AddPasswordAsync(newUser, model.Password);

                    if (passwordResult.Succeeded)
                    {
                        Braintree.CustomerSearchRequest search = new Braintree.CustomerSearchRequest();
                        search.Email.Is(model.Email);
                        var searchResult = await _braintreeGateway.Customer.SearchAsync(search);

                        if (searchResult.Ids.Count == 0)
                        {
                            //Create  a new Braintree Customer
                            await _braintreeGateway.Customer.CreateAsync(new Braintree.CustomerRequest
                            {
                                Email     = model.Email,
                                FirstName = model.FirstName,
                                LastName  = model.LastName,
                                Phone     = model.PhoneNumber
                            });
                        }
                        else
                        {
                            //Update the existing Braintree customer
                            Braintree.Customer existingCustomer = searchResult.FirstItem;
                            await _braintreeGateway.Customer.UpdateAsync(existingCustomer.Id, new Braintree.CustomerRequest
                            {
                                FirstName = model.FirstName,
                                LastName  = model.LastName,
                                Phone     = model.PhoneNumber
                            });
                        }

                        var confirmationToken = await _signInManager.UserManager.GenerateEmailConfirmationTokenAsync(newUser);

                        confirmationToken = System.Net.WebUtility.UrlEncode(confirmationToken);

                        string     currentUrl      = Request.GetDisplayUrl();               //get Url for current request
                        System.Uri uri             = new Uri(currentUrl);                   //Wrap in a URI object so I can split it into parts
                        string     confirmationUrl = uri.GetLeftPart(UriPartial.Authority); //Gives scheme and authority of the URI
                        confirmationUrl += "/account/confirm?id=" + confirmationToken + "&userId=" + System.Net.WebUtility.UrlEncode(newUser.Id);

                        await this._signInManager.SignInAsync(newUser, false);

                        var emailResult = await this._emailService.SendEmailAsync(
                            model.Email,
                            "Welcome to the Country Club",
                            "<p>Thanks for signing up, " + model.UserName + "!</p><p><a href=\"" + confirmationUrl + "\">Confirm your account<a></p>",
                            "Thanks for signing up, " + model.UserName + "!"
                            );

                        if (!emailResult.Success)
                        {
                            throw new Exception(string.Join(',', emailResult.Errors.Select(x => x.Message)));
                        }
                        return(RedirectToAction("Index", "Home"));

                        //if (emailResult.Success)
                        //{
                        //    return RedirectToAction("Index", "Home");
                        //}
                        //else
                        //{
                        //    return BadRequest(emailResult.Message);
                        //}
                    }

                    else
                    {
                        foreach (var error in passwordResult.Errors)
                        {
                            ModelState.AddModelError(error.Code, error.Description);
                        }
                    }
                    //can now create a user and log in their info
                }
                else
                {
                    foreach (var error in creationResult.Errors)
                    {
                        ModelState.AddModelError(error.Code, error.Description);
                    }
                }
            }
            return(View());
        }