mercredi 4 novembre 2015

Laravel Angular CORS issue in Authentication

I am following this tutorial to set up authentication with tokens using Laravel and Angular.

http://ift.tt/1FNj4m6

This works fine but when I host Laravel (as backend) separately, and Angular frontend on another domain, I get the foolowing error in console :-

XMLHttpRequest cannot load http://ift.tt/1H6w7qq.
Response to preflight request doesn't pass access control check: The 
'Access-Control-Allow-Origin' header contains multiple values '*, *', but 
only one is allowed. Origin 'http://ift.tt/1RRQisF' is 
therefore not allowed access.

I've placed a CORS middleware in Laravel and it works fine for simple routes.

class Cors
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    return $next($request)->header('Access-Control-Allow-Origin', '*')-  >header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}

}

How do I add CORS middleware to this :-

Route::group(['prefix' => 'api'], function()
{
    Route::resource('authenticate', 'AuthenticateController', ['only' =>    ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});

Adding it next to ['prefix' => 'api'] did not solve the problem.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire