public function register(User $user) { if ($this->exists($user->getUsername())) { throw new \Exception("User already registered"); } $hashedPass = password_hash($user->getPassword(), PASSWORD_DEFAULT); $result = $this->db->prepare("\n INSERT INTO users (username, password, role_id, cash)\n VALUES (?, ?, ?, ?);\n "); $result->execute([$user->getUsername(), $hashedPass, $user->getRole(), $user->getCash()]); if ($result->getAffectedRows() > 0) { return true; } throw new \Exception('Cannot register user'); }
private function initLogin($username, $pass) { $user = new User(); $user->setUsername($username); $user->setPassword($pass); $userId = $this->data->login($user); if ($userId) { $this->session->userid = $userId; $this->session->csrf = md5(uniqid(rand(), true)); } else { throw new \Exception('Cannot login user'); } header('Location: /'); exit; }