Check login in Laravel 5.7 from external script

2

I have an application in Laravel and I need to check, from an external script, if a user is logged in or not and what it is. For this I use the following lines of code, to load Laravel:

require_once __DIR__.'/../../../vendor/autoload.php';

$app = require_once __DIR__.'/../../../bootstrap/app.php';

$app->make('Illuminate\Contracts\Http\Kernel')
    ->handle(Illuminate\Http\Request::capture());

/*if (Cookie::get(config('session.cookie')) != "") {
    $id = Cookie::get(config('session.cookie'));
    Session::driver()->setId($pericod);
    Session::driver()->start();
}*/

$isAuthorized = Auth::check();
if(!$isAuthorized){
    echo "NO AUTORIZADO";
    exit();
}

With these lines I have access to any function of Laravel and in fact, the login check works correctly for GET requests, but when it is a request POST fails me, it is not able to recognize the login, from fact, I think he restarts the session because he is not able to recover the one that existed.

I have done a lot of tests and I think that it does not load or use certain parts of the Laravel, for example, it does not recognize the routes, it does not load the middlewares, etc ...

Use Laravel 5.7 updated today and this same code has come to work for me, I remember that in Laravel 5.4.

Any ideas of what may be failing?

thank you very much.

    
asked by Álvaro 17.09.2018 в 10:49
source

1 answer

1

I discovered the problem.

The trick was that the path of the php file that I post is external to laravel, that makes the route resolve of laravel identify it as / is. Everything was working with requests GET because in my routes.php file of Laravel I have the path / as type get. If I change the path type to any, everything works correctly.

I was not seeing the problem because I was not finishing Laravel's execution. If I change the logged user verification part, if I can see the error:

$isAuthorized = Auth::check();
if(!$isAuthorized){
    echo "NO AUTORIZADO";

    $response->send();
    $kernel->terminate($request, $response);

    exit();
}

These two lines complete the execution of Laravel, revealing the real problem, showing the error: "405 Method Not Allowed".

$response->send();
$kernel->terminate($request, $response);

Thank you very much for your help.

    
answered by 04.10.2018 в 16:31