I have a Laravel app running on Azure Web App with MySQL Flexible Server. When I try to make POST requests, I get an error saying “The XSRF-TOKEN cookie was rejected for having an invalid domain.”
The app works fine locally but fails on Azure. I suspect it might be related to HTTPS/HTTP configuration or session settings. The browser shows the connection as not secure.
My authentication request class:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Validation\ValidationException;
class AuthRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'email' => ['required', 'string'],
'password' => ['required', 'string']
];
}
public function attemptLogin(): void
{
if(!Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))){
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => 'Invalid credentials',
'password' => 'Wrong password',
]);
}
RateLimiter::clear($this->throttleKey());
}
public function throttleKey(): string
{
return Str::lower($this->string('email')).'|'.$this->ip();
}
}
Controller method:
public function handleLogin(AuthRequest $request)
{
$request->attemptLogin();
$request->session()->regenerateToken();
return redirect()->route('home');
}
Routes:
Route::get('/auth', [AuthController::class, 'show'])->name('auth');
Route::post('/auth', [AuthController::class, 'handleLogin'])->name('authenticate');
What Azure configuration changes do I need to make this work? Should I modify session settings or add specific environment variables?