<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Admin\Session\SessionIdController;
use App\Models\Fail;
use Closure;

class AuthenticateMiddleware
{
    public function handle($request, Closure $next)
    {
        if ($request->header("Token") == "") {
            // return json_encode( new Fail (
            //     success:false,
            //     error:"missing header token"
            // ));
            return response(json_encode( new Fail (
                success:false,
                error:"missing header token")),401);
        }
        $result=SessionIdController::staticIndex($request->header('token'));
        if($result->success==false){
            return response(json_encode( new Fail (
                success:false,
                error:$result->errorMessage
            )), 401);
        }
        return $next($request);
    }
}
