File: /var/www/farm.kosmicfarms_backup/app/controllers/SessionController.php
<?php
use Phalcon\Mvc\Controller;
class SessionController extends Controller
{
public function indexAction()
{
$this->view->pick('session/index');
$this->assets->collection('nucleo')
->addCss('assets/css/nucleo-icons.css')
->addCss('assets/css/nucleo-svg.css');
$this->assets->collection('pagestyle')
->addCss('assets/css/material-dashboard.css?v=3.2.0', true, false, ['id' => 'pagestyle']);
}
public function loginAction()
{
if ($this->request->isPost()) {
$email = $this->request->getPost('email');
$password = $this->request->getPost('password');
// Find the user by email/username and check password
$user = Users::findFirst([
"(email = :email:) AND active = 'Y'",
'bind' => ['email' => $email],
]);
if ($user && $this->security->checkHash($password, $user->password)) {
// Store user identity and role in the session
$this->session->set('auth', [
'id' => $user->id,
'name' => $user->name,
'role' => $user->role, // Assign the role from the database
]);
// Redirect to a protected area, e.g., 'dashboard'
return $this->response->redirect('dashboard');
}
}
$this->flashSession->error('Incorrect credentials. Please try again.');
// Disable the view and redirect back to the login page
$this->view->disable();
return $this->response->redirect('login');
// Render the login form
}
public function logoutAction()
{
$this->session->destroy();
$this->flash->success('You have been logged out.');
return $this->response->redirect('index/index');
}
}