Home > Blockchain >  Laravel automatic log in when redirect to subdomain
Laravel automatic log in when redirect to subdomain

Time:11-01

I have 2 subdomains with different databases but some users are listed in both database. These users have the right to switch to another subdomain, but when they want to switch to the other, they have to log in again. I want them to be able to switch without logging in, how could I do that? I've already found some information about this but it didn't help unfortunately

i try this: -Controller:

function change_database(Request $request)
    {
        $email=Auth::user()->email;
        $password=Auth::user()->password;
        return Redirect::to($request->selected_database)->with([
            'email' => $email,
            'password' => $password
            ]);
    }

-web.php:

Route::get('/', function () {
    if(isset($email) && isset($password)){
        Auth::attempt(['email' => $email, 'password' => $password]);
        

    }
    return view('auth.login');
});


CodePudding user response:

Quick question, have you set session domain in ENV file to ".yoursite.com"? The '.' at the start will allow sessions to exist among main/subdomains.

CodePudding user response:

Not sure if this serves the purpose but have you tried the below scenario?

1- To switch, the URL on web.php is let's say /switch which takes user details as parameter and hits controller method 2- Controller method verifies if the authentication still exist (to be sure the request is valid). Then, kill auth and attempt to login the with the user details passed - once authenticated, return redirect to the required page.

Is this exactly what you are trying to achieve?

  • Related