Пример #1
0
        public IActionResult Register([Bind("Name,Surname,Adress,Email,Password")] User user)
        {
            if (HttpContext.Session.GetObjectFromJson <User>("user") == null || HttpContext.Session.GetObjectFromJson <User>("user").AccountType != 1)
            {
                return(RedirectToAction("Games", "Home"));
            }
            else
            {
                if (!ModelState.IsValid)
                {
                    return(View(user));
                }
                else
                {
                    if (EmailExist(user.Email))
                    {
                        ModelState.AddModelError("Email", "Korisnik s tom email adresom vec postoji!");
                        return(View(user));
                    }
                    else
                    {
                        int id = _context.Users.LastOrDefault().Id + 1;
                        user.Id          = id;
                        user.Password    = Enkripcija.Hash(user.Password);
                        user.AccountType = (int)AccountType.Admin;

                        _context.Users.Add(user);
                        _context.SaveChanges();
                        return(RedirectToAction("Login", "Home"));
                    }
                }
            }
        }
Пример #2
0
        public IActionResult Login([Bind("Email,Password")] LoginUser user)
        {
            if (!ModelState.IsValid)
            {
                return(View(user));
            }
            else
            {
                if (!EmailExist(user.Email))
                {
                    ModelState.AddModelError("Email", "Nepostojeći email");
                    return(View(user));
                }
                else
                {
                    User logUser = _context.Users.FirstOrDefault(x => x.Email == user.Email);

                    if (Enkripcija.Hash(user.Password) != logUser.Password)
                    {
                        ModelState.AddModelError("Password", "Kriva zaporka");
                        return(View(user));
                    }
                    else
                    {
                        if (logUser.AccountType == 1)
                        {
                            HttpContext.Session.SetObjectAsJson("user", logUser);
                            return(RedirectToAction("Games", "Admin"));
                        }
                        else if (logUser.AccountType == 2)
                        {
                            ModelState.AddModelError("Password", "Korisnik nije administrator");
                            return(View(user));
                        }
                    }
                    return(View(user));
                }
            }
        }