问题
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?
回答1:
In config/session.php
you should change:
'domain' => null,
into
'domain' => '.example.ca',
You should also clear all the cookies
回答2:
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
回答3:
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::
回答4:
For anyone getting the same error, I followed all above until I did these:
Choose either database or redis as your session driver. I chose redis and I input that into my env and and even into the default of config.session.driver, for good measure.
I changed the domain name to '.domain.com' as all have suggested above.
However, having done all that, strangely, there was no change, until I changed the session cookie name. By default it has: 'APP_NAME', 'Laravel'), '_').'_session' I only changed the 'Laravel' part to my domain...
Lastly, clearing the cookie from the conventional way didn't work. At least for me using firefox. I had to open up the dev tools and delete all the sites entry under Storage > Cookies And like magic, boom! It worked! Try it.
来源:https://stackoverflow.com/questions/34488477/authuser-doesnt-travel-across-subdomains-laravel-5-2