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
/homeroute intool.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:
- Sharing the cookies, sessions and users between the applications
-
Setting the same
APP_KEY -
Setting the same
SESSION_DOMAINwith 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.comwe're usingexampleGuards withexampleProvider usingExampleModel 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.comI've assigned the$tablevariable asexampleinUserModel to accessexampletable 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
exampletable 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.
Please or to participate in this conversation.