50$ : Laravel 5.8 - Cannot share session nor cookies across domains that points to same application
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hello everyone, I have two domains that point to the same application. One is the English version and the other is the French version. In AppServiceProvider I have: if(URL::to('/') === 'http://site-in-english.test') {
config(['app.locale' => 'en']);
} else {
config(['app.locale' => 'fr']);
}
and that works fine. However my session and cookies are not share between domains, although it’s the same application. Anyone would have a solution for this ? I'm looking for a clean solution.

To debug this, you'll need to create a Laravel 5.8 app and have two domains pointing to that app, then add the code above in your AppServiceProvider. Then you'll be able to see that cookies are not shared between the two domains even though it's in the same application.

I'm offering a 10$ bounty but I will pay an extra 40$ to anyone who find the solution.

By default lavarel sets cookies for the specific subdomain, hence it wouldn't share across other subdomains. You'll need to adjust either session.php settings or Cookie:queue method (setting cookie path/domain to .domain.com) to share cookies across subdomains. Have a look at this article for more information. I can post this as a solution if you find this helpful.
Wuddrum 2 months ago
Hi there !! Thanks for the help, I tried it but in my case the other domain is not a subdomain, but really a different domain that points to the same app.
randomhelprequested 2 months ago
Well in such case you'd have to either set cookies twice for both separate domains or use a technique that's similar (or exactly) like this
Wuddrum 2 months ago
awarded to radosinsky

Crowdsource coding tasks.

1 Solution

Winning solution

Hello, one of the solutions would be doing this:

Add this to your HTML:
<script src="http://theotherwebsite.test/set-selected-group-cookie/45180645"></script>

And this to your routes:
Route::get('/set-selected-group-cookie/{group_code}', 'GroupController@setSelectedGroupCookie')->name('setSelectedGroupCookie');

Then add this to your controller:

public function setSelectedGroupCookie($group_code) : void {
  $group = Group::where('code', $group_code)->first();
  Cookie::queue('selected_group', $group, 30);
}

Let me know if you'd want help implementing this to your project or if you have any questions. Thank you.