<?php

namespace routes;

use classes\authentication;
use objects\logs_o;
use traits\route_t;

class sessionRoute
{
    use route_t;

    public function run(): void
    {
        $this->get('/auth/session', function () {
            // Require the user to be logged in
            global $response;
            $this->requirePermission('fetch_session');
            // Get the user object
            $user = (new authentication())->get_user();
            // Check if the request was successful
            if ($user) {
                // Log the incident
                (new logs_o())->add('auth', 'global', 1, $user->id, 'FETCH_SESSION', 'User id: ' . $user->id);
                // Return the user object
                $response->success($user);
            } else {
                // Log the incident
                (new logs_o())->add('auth', 'global', 1, 0, 'FETCH_SESSION_FAILURE', 'No user found, or invalid session');
                // Return an error
                $response->error('Invalid session', 400);
            }
        },
            [
                'fetch_session' => 'Fetch session'
            ]
        );
    }
}