HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33
System: Linux li317-225.members.linode.com 3.10.0-1062.12.1.el7.x86_64 #1 SMP Tue Feb 4 23:02:59 UTC 2020 x86_64
User: apache (48)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //var/www/farm.kosmicfarms/app/controllers/SessionController.php
<?php

use Phalcon\Mvc\Controller;
use Phalcon\Http\Cookie;
use Phalcon\Debug;

class SessionController extends Controller
{
   
    
    public function initialize()
    {
        $this->view->setVar('body_css', "bg-gray-100");
        $this->view->setVar('no_Nav', true);
    }
    
    public function indexAction()
    {
        // Check if session exists
        if ($this->session->has('auth')) {
            // Redirect to dashboard or home page
            return $this->response->redirect('/');
        }
        $this->view->pick('session/index');   
    }
    
    public function loginAction()
    {
        
        // Check if session exists
        if ($this->session->has('auth')) {
            // Redirect to dashboard or home page
            return $this->response->redirect('/');
        }
       
        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
                $roleNames = [];
                foreach ($user->roles as $role) {
                    $roleNames[] = $role->name;
                }
                
                $this->session->set('auth', [
                    'id'   => $user->id,
                    'fname' => $user->fname,
                    'lname' => $user->lname,
                    'roles' => $roleNames, // Assign the role from the database
                ]);
                
                // In your loginAction
                if ($this->request->getPost('remember')) {
                    $token = $this->security->getRandom()->hex(16); // Generate token
                    // Save $token to 'remember_tokens' table linked to user_id
                    
                    // Set cookie for 1 month (30 days)
                    $this->cookies->set(
                        'RM',
                        $user->id . ':' . $token,
                        time() + 30 * 86400
                        );
                }
               
                // 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()
    {
        
        return $this->myHelpers->logout();
    }
}