Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maq5ud's avatar

Unable to retrieve session in sub-domain

Description :

I've two Laravel application on same server but on different path. And I want to merge both application by sharing session.

Both of these application has domain and sub-domain combination namely:

  • example.com
  • tool.example.com

And I'd like authenticate user in tool.example.com from authenticating in example.com using following articles.

Note: That I'm not passing any username and password while accessing the /home route in tool.example.com.

Problem Statement:

However, while accessing the route /home in tool.example.com giving me Unauthenticated.

Exception has occurred. Illuminate\Auth\AuthenticationException: Unauthenticated.

I tried the following:

  1. Sharing the cookies, sessions and users between the applications
  2. Setting the same APP_KEY
  3. Setting the same SESSION_DOMAIN with prefix .

example.com Laravel application I did the following:

.env

DB_DATABASE=common_database
SESSION_DRIVER=database
APP_KEY=same_key
SESSION_DOMAIN=".example.com"
  • Storing session in database. link

  • In example.com we're using example Guards with example Provider using Example Model to authenticate web request.

config/auth.php

    'guards' => [
        'example' => [
            'driver' => 'session',
            'provider' => 'example',
        ],
    ],

    'providers' => [
        'example' => [
            'driver' => 'eloquent',
            'model' => App\Example::class,
        ],
    ],

Similarly, tool.example.com :

.env

DB_DATABASE=common_database
SESSION_DRIVER=database
APP_KEY=same_key
SESSION_DOMAIN=".example.com"
  • In tool.example.com I've assigned the $table variable as example in User Model to access example table to authenticate the request.

User.php

class User extends Authenticatable 
{
    protected $table = 'example';

config/auth.php

    'guards' => [
        'example' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],

Note: I'm expecting to use example table to authenticate request in both application.

EDIT 1:

After, dd($request->session()->all()); in example.com

array:5 [▼
  "_token" => "tokenid_1"
  "_previous" => array:1 [▶]
  "_flash" => array:2 [▶]
  "login_example_longdigitnumber" => 9
  "example_id" => 9
]

While, dd($request->session()->all()); in tool.example.com

array:3 [▼
  "_token" => "tokenid_2"
  "url" => array:1 [▶]
  "_flash" => array:2 [▶]
]

Any help is greatly appreciated. Thanks! in advance.

0 likes
0 replies

Please or to participate in this conversation.