mdev11's avatar
Level 1

Login from main domain to a subdomain

I want to be able to log in from example.com to subdomain.gecegypt.com.

I have multiple databases connected to the main domain config/database.php:

'mysql' => [
    'driver' => 'mysql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
],

'subdomain' => [
    'driver' => 'mysql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('SUBDOMAIN_DB'),
    'username' => env('SUBDOMAIN_USERNAME'),
    'password' => env('SUBDOMAIN_PASSWORD'),
],

model/User.php:

protected $connection = 'subdomain';

Controllers/LoginController.php:

public function login(LoginRequest $request)
{
    if(Auth::attempt($request->validated()))
    {
        $request->session()->regenerate();
        
        return redirect('https://subdomain.example.com');
    }
}

.env:

APP_URL=https://example.com

SUBDOMAIN_DB=db
SUBDOMAIN_USERNAME=user
SUBDOMAIN_PASSWORD=password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_DOMAIN=subdomain.example.com

After entering the correct login credentials, I'm redirected to subdomain.example.com/login. I tried to put the values of APP_URL and SESSION_DOMAIN to the subdomain link. I also tried to add/remove https from the links. As well as, setting SESSION_SECURE_COOKIE=false in .env file. None of these solutions worked.

How to log in from the main domain to a subdomain?

Note: There are other subdomains with different databases and login forms

0 likes
5 replies
mdev11's avatar
Level 1

@jlrdw I tried all the solutions from that post but unfortunately, it didn't work.

jlrdw's avatar

@mdev11 is the .env SESSION_DOMAIN correct? Shouldn't it be the main domain. Where is the session table.

mdev11's avatar
Level 1

@jlrdw I tried to put it equal to .example.com. I even tried to use the same APP_KEY, APP_NAME, and SESSION_DOMAIN the same for both main and subdomain. I'm using file sessions not database.

jlrdw's avatar

@mdev11 Try database. The first link explained you need database sessions.

Please or to participate in this conversation.