/** * prompts the user to login if it exists in the database, else it prompts for registration */ public function auth() { if (!$this->user->isLoggedIn()) { if ($this->user->exists()) { // load the login template $view = 'admin/login'; // users exist, set up the login verification process // if theres input if (Input::exists()) { // get input values $username = Input::get('username'); $password = Input::get('password'); // check if a unique token is set if (Token::check(Input::get('token'))) { // validate the form $this->validator->validate(['username' => [$username, 'required'], 'password' => [$password, 'required']]); if ($this->validator->passes()) { // log the user in if ($this->user->login($username, $password)) { header('Location: /admin/index'); } } } } // delete the flash message that occurs after registering an account if (Session::exists('success')) { $flash = Session::flash('success'); } } else { // load the registration template $view = 'admin/register'; // no users exist, set up the registration process // if theres input if (Input::exists()) { // get input values $username = Input::get('username'); $password = Input::get('password'); $password_confirmation = Input::get('password_confirmation'); // check if a unique token is set if (Token::check(Input::get('token'))) { // validate the form $this->validator->validate(['username' => [$username, 'required|alnumDash|min(3)|max(25)'], 'password' => [$password, 'required|min(8)'], 'password_confirmation' => [$password_confirmation, 'required|matches(password)']]); if ($this->validator->passes()) { // validation passed, insert a new user to the database $this->user->create($username, Hash::hashPassword($password)); Session::flash('success', 'Your account has been successfully created.'); header('Location: /admin/auth'); } } } } // render the right view $this->view($view, ['flash_message' => isset($flash) ? $flash : '', 'validation_errors' => $this->validator->errors(), 'csrf_token' => Token::generate(), 'user_error' => $this->user->auth_error_message]); } else { // the user is already logged in header('Location: /admin/index'); } }
/** * logges the user out */ public function logout() { // delete the session Session::delete($this->session_name); }