Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auth::user() returns null across domain, in a laravel application

i have a laravel application hosted on a server a.com, and this application handles all the authentications for other laravel applications setup on other servers, Server 1 - app.com - does the authentication/user management for the system and stores it in a cookie to be sent across to servers 2,3 and 4,

server 2. mail.app.com  
server 3. fms.app.com  
server 4. dod.app.com 

There is an initialize function on servers 2,3 and 4 that tries to decode the cookies sent across the domains from server 1. which looks something like this.

    public function initialize(){
      $mail = json_decode($logged_in_user_cookie)->email;
      $user = User::where('email', $mail)->first();    
      if(!$user){
        $user = new User;
        $user->email = $mail;
        $user->save();
      }
      Auth::login($user);

      if(Auth::check()){
      //dd($user); - works fine..
      return redirect()->route('dashboard');

      }else{
        echo 'user not logged in ';
      }

    }

Servers 2, 3, and 4 also has users table but without a password, so if a cookie hits any of these servers, the system reads the cookie and extracts the user object from the cookie and checks if any user does exists, creates the user and then uses the [ Auth::login($user) ] to login the user into the current system, and if the user already exists.. it automatically logs in the user..

now the problem we are having is, on this line return redirect()->route('dashboard'); It redirects you to the dashboard page of the application, and dd(Auth::user()) - it returns null,

and we are not able to figure out why it is working that way. since the Auth::user(), should be available across the entire application, Just think of it like how google works,

google.com, - one login controls every application including youtube drive.google.com, mail.google.com, play.google.com, news.google.com, plus.google.com, youtube.com - that is what we are trying to do.

like image 577
Ande Caleb Avatar asked Oct 29 '22 22:10

Ande Caleb


1 Answers

Go to your config/session.php and rename this 'cookie' => 'laravel_session' to your session name. For example 'cookie'=>'foo_session'. This should work.

like image 73
Olasunkanmi Avatar answered Nov 11 '22 17:11

Olasunkanmi