sam sam sam sam - 3 months ago 29
PHP Question

How to check auth user in laravel subdomain?

I have a project it's Backend is built in Laravel 5.2 and Frontend is built in PHP. Please check the folder structure below it's in localhost.

Backend : http://crm.test.dev This is the root folder, laravel5.2 -> public ->index.php

Frontend: http://test.dev This url is point to laravel5.2 -> public -> frontend -> index.php

My purpose is, If a user(admin) is logged in Backend(http://crm.test.dev) and not signed out. The same user is accessing Frontend(http://test.dev") at that time I need to show an alert you are logged in Backend(http://crm.test.dev).

frontend->index.php

require __DIR__.'/../../bootstrap/autoload.php';
$app = require_once __DIR__.'/../../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);

$isAuthorized = Auth::check();
$user = Auth::user();
echo $user;

Answer

Actually issue is from config -> session.php {'domain' => null,}. I updated this line to { 'domain' => '.test.dev', } . Now a user is logged in backend and accessing frontend (subdomain: http://test.dev) "Your are logged in" message shows.

require __DIR__.'/../../bootstrap/autoload.php';
$app      = require_once __DIR__.'/../../bootstrap/app.php';
$kernel   = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$isAuthorized = Auth::check();
$user         = Auth::user();

if(Auth::check()) {
    echo "Your are logged in";
}
else {
    echo "Sorry";
    }
Comments