<?php
class LoginController{
    public function getToken(){
        global $user;
        if(isset($_POST['login'])){
            $login = $user->login($_POST['username'], $_POST['password'], true);

            echo json_encode($login);
        }else{
            echo json_encode(['status' => 'fail', 'errorMessage' => 'invalid request']);
        }
    }

    public function validateToken(){
        global $user;
        if($user->isLoggedIn()){
            $message = ['status' => 'success'];
        }else{
            $message = ['status' => 'fail', 'errorMessage' => $user->getErrorMessage()];
        }

        echo json_encode($message);
    }

    public function getModules(){
        global $user, $route;
        if($user->isLoggedIn()){
            echo json_encode(['modules' => $route::getModules()]);
        }else{
            echo json_encode(['modules' => []]);
        }
    }

    /**
     * open packages page
     */
    public function showPage(){
        global $user;
        require_once('LoginPage.php');
    }
}
?>
