Search code examples
laravelmulti-tenant

how to login to main domain and subdomain multi tenant?


i am using package for multi-tenant

multi tenant for spatie

my system have that :

every user has it's own database and subdomain,

i need when the user login in main domain and redirect to it's subdomain keep logged in.

i have tried

config(['session.domain'=>'.example.com']);

and

config(['session.domain'=>'*.example.com']);

and i set the config session itself

and set

session_driver => 'cookie'
session_driver=>'file'

thanks.


Solution

  • to help those who will have save issue, i just did next

    in main login i did that

    \DB::purge('tenant');
    Config::set('database.connections.tenant.database', 'main_database');
    $user = auth()->user();
    $token = Str::random(64);
    $user->login_token = $token;
    $user->save();
    \DB::purge('tenant');
                                
    Config::set('database.connections.tenant.database', Tenant::where('domain', 
    auth()->user()->domain)->first()->database);
    User::where('email', $user->email)->first()->update(['login_token' => $token]);
    return redirect()->to('http://' . auth()->user()->domain . '.' . env('APP_URL') . "/login/$token");
    

    and i login get i did that

    public function loginGet($token = null)
    {
    
        if ($token) {
            $user = User::where('login_token', $token)->first();
            Auth::loginUsingId($user->id);
            $user->login_token = null;
            $user->save();
            return redirect()->to('/dashboard');
        }
    
        return view('auth.login');
    }