Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

george1's avatar

Login cross subdomain in Laravel 9 issues

Hi there,

I am building a website on Laravel 9 which will have 2 subdomains (maybe more in the future!) which are accessible depending on the role of the user. Below, I have the subdomains which are accessible by the corresponding user.

examplesite.com - accessible by the admin, contributor, user
contributor.examplesite.com - accessible by contributor
admin.examplesite.com - accessible by the admin

The type of the user is determined by the role column in the users table.

examplesite.com is used for the home page and any other relevant pages which can be viewed by anyone regardless if they are authenticated or not.

contributor.examplesite.com is used only for users who have been registered as a contributor where they can deal with uploading content on the website.

admin.examplesite.com is used only for admin users.

Currently, if I login as a contributor, then only the pages which are accessible under the contributor authenticated group will be accessed, but wont be able to access any authenticated pages for the non subdomain pages. Same goes with the admin.

So far I have tried to add a session domain under the .env file SESSION_DOMAIN=.examplesite.com, but still nothing is working.

Here's the web.php file where all the routes are located.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Middleware\isAdmin;
use App\Http\Middleware\Authenticate;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// contributor routes

Auth::routes();

Route::domain('contributor.examplesite.com')->group(function () {

    Route::middleware([isContributor::class])->group(function () {

        Route::get("/upload", [App\Http\Controllers\ImageController::class, 'create'])->name('image.update');
        Route::post("/upload/store", [App\Http\Controllers\ImageController::class, 'upload'])->name('image.upload.store');
        Route::get("/upload/info", [App\Http\Controllers\ImageController::class, 'add_meta'])->name('image.upload.add_meta');
        Route::post("/upload/submit", [App\Http\Controllers\ImageController::class, 'store'])->name('image.upload.submit');
        Route::get("/upload/pending", [App\Http\Controllers\ImageController::class, 'pending'])->name('image.upload.pending');

        Route::post("/keywords/get", [App\Http\Controllers\KeywordController::class, 'getKeywordsByCategory'])->name("keywords.category.get");
    });
});

Route::domain('admin.examplesite.com')->group(function () {

    Route::middleware([isAdmin::class])->group(function () {

        Route::get("/users", [App\Http\Controllers\UserController::class, 'index'])->name('user.index');
        Route::get("/user/{id}/content/images/{status}", [App\Http\Controllers\ImageController::class, 'index'])->name('user.content.images');
        
        Route::post("/user/{id}/content/images/reject", [App\Http\Controllers\ImageController::class, 'reject'])->name('user.content.images.reject');
        Route::post("/user/{id}/content/images/approve", [App\Http\Controllers\ImageController::class, 'approve'])->name('user.content.images.approve');

    });
});


Route::domain('examplesite.com')->group(function () {


    Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    // Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('welcome');

});

Route::middleware([Authenticate::class])->group(function () {

    Route::get('/logout', [App\Http\Controllers\Auth\LoginController::class, 'logout']);

});

Please, let me know what I am missing, thanks!

0 likes
0 replies

Please or to participate in this conversation.