I'm working on a Laravel 9, InertiaJS and vue project where a user can submit a new product through products/create
route and this sends to ProductController@store which does all what it's supposed to do and then supposedly redirect the user to the newly created product on a subdomain route like this http://username.example.test/my-new-product
The issue I'm having is that I keep getting the below error and the redirect doesn't work:
Access to XMLHttpRequest at 'http://user.example.test/my-first-product' (redirected from 'http://example.test/products/create') from origin 'http://example.test' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
The code works as follows:
1- The form:
const form = useForm("createProduct", {
webshop_id: null,
title: null,
price: null,
type: null,
currency: null,
description: null,
thumbnail: null,
});
const submit = () => {
form.post(route("products.create"), {
onSuccess: () => form.reset(),
});
};
2- Products create router:
Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified',
])->group(function () {
Route::post('products/create', [ProductController::class, 'store'])->name('products.create');
});
3- The controller - Storing the new product with user details and redirecting (which doesn't work) to show the product
public function store(StoreProductRequest $request)
{
// Code
return redirect()->route('products.show', [$user, $product]);
}
4- The show route and controller@show
Route::domain('{user:username}.' . env('APP_URL'))->group(function () {
Route::get('{product:slug}', [ProductController::class, 'show'])->name('products.show');
});
public function show(User $user, Product $product)
{
return Inertia::render('Products/Show', [
'user' => $user,
'product' => $product,
'thumbnails' => $product->productimages
]);
}
I saw @reinink explain in this Issue that we can use now Inertia::location
The only way I could get this to work is returning this is the ProductController@store:
return Inertia::location('http://' . $user->username . '.example.test/' . $validated['slug']);
But It causes a full page refresh which defeats the whole concept of having a single page application and I will have to remember changing 'http' to 'https' and domain to the real domain when I'm going to put the app in production. I was hoping that I was doing something wrong but after hours of searching for a solution I began to think that Laravel Inertia apps doesn't support subdomain redirects