An older post but might help: https://laracasts.com/discuss/channels/laravel/how-to-share-session-between-maindomain-and-subdomain
Jul 4, 2024
5
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
Please or to participate in this conversation.