I'm very new to laravel
and created my first REST api
project with it, then I set cors
middleware and it works perfectly, one thing is so important to me, that prevent open route with GET
method in browser directly, I don't know how to allow api
return data only when it called form my own website not directly in browser. I googled and found this solution
public function handle($request, \Closure $next)
{
if ( ! $request->ajax())
return response('Forbidden.', 403);
return $next($request);
}
So it works fine and if you open route in browser directly like site.com/api/car/index
it return 403
error, but it return this error in my website too! my website created with reactjs/nextjs
and use axios
to call api
, any idea how to fix this? or is there better solution to avoid this problem?