Пример #1
0
        public async Task <IActionResult> Register([FromForm] Register register,
                                                   [FromServices] CartMapper mapper)
        {
            if (!ModelState.IsValid)
            {
                return(CreatedAtAction("Register", register));
            }

            User user = await _context.Users.FirstOrDefaultAsync(u => u.Email == register.Email);

            if (user == null)
            {
                var userEntry = await _context.Users.AddAsync(new User
                {
                    Email        = register.Email,
                    PasswordHash = register.Password
                });

                await Authenticate(GenerateUserClaims(userEntry.Entity));

                await mapper.MapTo(userEntry.Entity);

                return(RedirectToAction("Index", "Home"));
            }
            ModelState.AddModelError("", "User already exists.");
            return(CreatedAtAction("Register", register));
        }
Пример #2
0
        public async Task <IActionResult> Login([FromForm] Login login,
                                                [FromServices] CartMapper mapper)
        {
            if (!ModelState.IsValid)
            {
                return(CreatedAtAction("Login", login));
            }

            User user = await _context.Users.FirstOrDefaultAsync(u => u.Email == login.Email);

            if (user != null)
            {
                await Authenticate(GenerateUserClaims(user));

                await mapper.MapTo(user);

                return(RedirectToAction("Index", "Home"));
            }
            ModelState.AddModelError("", "Incorrect login or password.");
            return(CreatedAtAction("Login", login));
        }