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

Binny Patel's avatar

CSRF token mismatch for domain redirects

Hello I am facing issue where I have concept of custom redirection in my project. After redirection there is a form submit process where I am facing CSRF token mismatch issue. I tried using cookie concept or replacing the request header token in session attribute in VerifyCSRFToken.php file. Later on I tried adding route to except param in same file But none of this worked. I would appreciate if I find the solution for this Thanks in advance

0 likes
16 replies
Binny Patel's avatar

VerifyCSRFToken.php :

protected $except = [
        '/auth/event',
        '/auth/catalog'
 ];

Route :

Route::group(['domain' => '{subdomain}.{domain}.{tld}', 'middleware' => ['catalog.identify',],
], function() {
Route::post('/auth/catalog', 'CatalogController')->name('catalog.auth');
Route::post('/auth/event', 'EventController')->name('event.auth');
});
gych's avatar

Do you get this issue when redirecting between different sub domains?

gych's avatar

Which domain have you set in session config?

Binny Patel's avatar

@gych my app domain. (note : and the custom domains or redirected domains would be dynamic and different for each of the users)

gych's avatar

Your session domain should be starting with a . for example .yourdomain.com

gych's avatar

Which session driver are you using?

Binny Patel's avatar

@amitsolanki24_ Same code There is a middleware for it where before fetching data we check whether its the app domain or custom domain

public function handle($request, Closure $next)
    {
        $domain = $request->route()->parameters['subdomain'] . '.' . $request->route()->parameters['domain'] . '.' . $request->route()->parameters['tld'];
        app()->singleton('domain', function () use ($request, $domain) {
            return [
                'fqdn' => $domain,
                'subdomain' => $request->route()->parameters['subdomain'],
                'domain' => $request->route()->parameters['domain'],
                'tld' => $request->route()->parameters['tld'],
            ];
        });

        // Custom domains
        if (!Str::contains($domain, config('_app.app_domain'))) {
            $photoCatalog = Query to fetch data;
        }

        // Subdomains
        if (empty($photoCatalog)) {
            if (!Arr::has($request->route()->parameters, 'subdomain')) {
                return abort(404);
            }

            $subdomain = $request->route()->parameters['subdomain'];

            if ($subdomain === 'domain') {
                return response()->redirectTo('https://app.' . config('_app.app_domain'));
            }

            $photoCatalog = Query to fetch data;
        }

        app()->singleton('photoCatalog', function () use ($photoCatalog) {
            return $photoCatalog;
        });

        return $next($request);
    }
gych's avatar

Use another session driver like database or cookie I remember someone having the same issue by using file as session driver in the past. Can't directly find the related post right now.

1 like

Please or to participate in this conversation.