Home » Php » Auth::user() doesn't travel across subdomains? – Laravel 5.2

Auth::user() doesn't travel across subdomains? – Laravel 5.2

Posted by: admin November 29, 2017 Leave a comment

Questions:

I have a subdomain in my site, for example:

http://example.ca
http://blog.example.ca

The blog one is only accessible to people who have the url, that is, its not public or common knowledge. I have a bunch of routes related to this domain that requires you to be logged in.

I have stated in a nav.blade.php that if the user is logged in, there should be a drop down and if not then there should not be a drop down.

this all works for http://blog.example.ca but when I go to http://example.ca I am not authenticated any more.

Is there any way in larval to make me authenticated across all subdomains? Not just the ones who have controller actions that require me to be authenticated?

Answers:

In config/session.php you should change:

'domain' => null,

into

'domain' => '.example.ca',

You should also clear all the cookies

Questions:
Answers:

In config/session.php you should change:

'domain' => null,

into

'domain' => '.example.ca',

and for main domain

'files' => storage_path('framework/sessions'), 

For subdomain I point to path of main domain ??/framework/sessions'.

After that, I can use Auth

Questions:
Answers:

just want to add to the answer

other than setting domain to ‘.domain.com’ in config/session, also make sure that both main domain and subdomain are accessing the same users table and the same sessions table

laravel stores the user ID in the session, so if you have different table, that ID won’t be found in the subdomain, thus not logged in and cannot access Auth::