Laravel login once and be loggined in in multiple subdomains

Viewed 795

our website is created with laravel 7 on an apache server. we have an app that has 3 different subdomains. for example:

mainwebsite.com

products.mainwebsite.com

customers.mainwebsites.com

suppliers.mainwebsite.com

the subdomains have been set this way :

Route::domain('supplier.mainwebsite.com')->group(function () {
      Route::get('/dashboard', 'ExampleController@show')->name('example-show')->middleware('auth','isActive','role:supplier|admin|producer');
});

we use spatie permission package to check the roles.

now the problem here is with authentication. if user is logged in to suppliers.mainwebsite.com, when they go to mainwebsite.com they should login again. we want them to be logged in in all the subdomains. because some of the pages are similar but show different content based on user's role. for example dashboard is in mainwebsite.com/dashboard but if you have logged in to suppliers.mainwebsite and there is a link to go to dashboard which goes to mainwebsite.com.dashboard because the subdomain is different they should login again. we want them to stay logged in.

I appreciate your help.

Regards,

1 Answers

try this

in config/session.php

'domain' => env('SESSION_DOMAIN', null),

to

'domain' => env('SESSION_DOMAIN', '.mainwebsite.com'),

or in .env

SESSION_DOMAIN='.mainwebsite.com',
Related