diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php new file mode 100644 index 0000000..f6b376f --- /dev/null +++ b/app/controllers/AuthController.php @@ -0,0 +1,54 @@ +set('error', $f3->get('SESSION.login_error')); + $f3->clear('SESSION.login_error'); + + // this can be in our controller base + $f3->set('content', '../ui/views/login.html'); + echo \Template::instance()->render('../ui/templates/layout.html'); + $f3->clear('error'); + } + + public function login($f3){ + $username = $f3->get('POST.username'); + $password = $f3->get('POST.password'); + + $db = $f3->get('DB'); + // query for user + $result = $db->exec( + 'SELECT id, username, password FROM users WHERE username =? LIMIT 1', $username + ); + + // verifiy password + if($result){ + $user = $result[0]; // first row + if(password_verify($password, $user['password'])){ + // valid + $f3->set('SESSION.user', [ + 'id'=> $user['id'], + 'username' => $user['username'] + ]); + + $f3->reroute('/dashboard'); + } else { + $f3->set('SESSION.login_error', 'Invalid password'); + } + } else { + // if here, login failed. + $f3->set('SESSION.login_error', 'Invalid username'); + } + $f3->reroute('/login'); + + } + + public function logout($f3){ + $f3->clear('SESSION'); + $f3->reroute('/'); + } + +} \ No newline at end of file diff --git a/app/controllers/DashboardController.php b/app/controllers/DashboardController.php new file mode 100644 index 0000000..f7d8948 --- /dev/null +++ b/app/controllers/DashboardController.php @@ -0,0 +1,9 @@ +set('content', '../ui/views/dashboard.html'); + echo \Template::instance()->render('../ui/templates/layout.html'); + } +} \ No newline at end of file diff --git a/app/controllers/HomeController.php b/app/controllers/HomeController.php index 61e3cd2..693eb1e 100644 --- a/app/controllers/HomeController.php +++ b/app/controllers/HomeController.php @@ -6,7 +6,7 @@ class HomeController { // $db = $f3->get('DB'); // echo \Template::instance()->render('../ui/views/home.html'); - echo \Template::instance()->render('../ui/views/home.html'); + echo \Template::instance()->render('../ui/templates/layout.html'); // Query // View diff --git a/public/index.php b/public/index.php index 7a58777..74b32c1 100644 --- a/public/index.php +++ b/public/index.php @@ -6,15 +6,32 @@ $f3 = \Base::instance(); $f3->set('DEBUG', 3); // development debug $f3->config('../app/.env.cfg'); +$f3->set('DB', new \DB\SQL( + 'mysql:host=localhost;port=3306;dbname=' . $f3->get('database.db_name'), + $f3->get('database.username'), + $f3->get('database.password') +)); + +new \DB\SQL\Session($f3->get('DB')); + // Routing and Controller Setup // home $f3->route('GET /', 'HomeController->display'); // auth -$f3->route('GET /login', 'Auth->login'); -$f3->route('POST /login', 'Auth->login'); -$f3->route('GET /logout', 'Auth->logout'); +$f3->route('GET /login', 'AuthController->showLoginForm'); +$f3->route('POST /login', 'AuthController->login'); +$f3->route('GET /logout', 'AuthController->logout'); + +// Example protected route +$f3->route('GET /dashboard', function($f3){ + if(!$f3->exists('SESSION.user')){ + $f3->reroute('/login'); + } + echo 'Welcome to the dashboard' . $f3->get('SESSION.username'); + echo 'logout'; +}); // tickets - CRUD (CREATE, READ, UPDATE, DELETE) $f3->route('GET /tickets', 'Tickets->list'); // view all tickets @@ -24,7 +41,6 @@ $f3->route('GET /ticket/@id', 'Tickets->read'); // view ticket details $f3->route('GET /ticket/@id/edit', 'Tickets->edit'); // edit ticket $f3->route('POST /ticket/@id/update', 'Tickets->update(PARAMS.id)'); // - // knowledgebase $f3->route('GET /kb', 'KB->list'); $f3->route('GET /kb/create', 'KB->create'); @@ -36,4 +52,6 @@ $f3->route('POST /kb/@id/edit', 'KB->update'); $f3->route('GET /tags', 'Tag->list'); $f3->route('POST /tag/create', 'Tag->create'); +$f3->route('GET /dashboard', 'DashboardController->index'); + $f3->run(); \ No newline at end of file diff --git a/ui/templates/layout.html b/ui/templates/layout.html new file mode 100644 index 0000000..0c7d3d3 --- /dev/null +++ b/ui/templates/layout.html @@ -0,0 +1,88 @@ + + + +
+ + +{{ @error }}
+