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

柔情痞子 提交于 2019-12-09 06:50:54

问题


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:

  1. 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.

  2. I changed the domain name to '.domain.com' as all have suggested above.

  3. 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...

  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!