Bump
Laravel 5.0 CORS and Angular
Im trying to make an api with my laravel app. My laravel app it's already on production so im making this new module to allow my mobile app get the info it needs. The problem as the title says, its the CORS validations. I already read a lot of post of the same topic and none of the answers provided were useful to me.
I already installed Barry solution for CORS and didn't work
Right now this is my setup
// routes.php
Route::group(['prefix'=>'api', 'middleware' => 'cors'], function(){
Route::post('login/auth', [function(){
if(Auth::attempt(Input::only('username','password'))){
return Auth::user();
}else{
return 'invalid username/pass combo';
}
}]);
});
//App\Http\Middleware
class Cors {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');
if (!$request->isMethod('options')) {
return $next($request);
}
}
}
//Kernel.php
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
'cors' => 'App\Http\Middleware\Cors',
];
And my mobile app, made using Ionic framework and Angular js is asking for data this way
$http.defaults.headers.common['X-CSRF-TOKEN'] = $('meta[name="csrf-token"]').attr('content');
$http({method:'POST',url:'http://someurl.com/api/login/auth',data:{username:name,password:pw}})
Now, the errors i get are the following
- With my current set up i get
XMLHttpRequest cannot load http://someurl.com/api/login/auth. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:85' is therefore not allowed access - If i put this on routes.php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');
got TokenMismatchException
- If i comment on Kernel.php VerifyCsrfToken AND keep the data on the previous step it works.
I cannot discard VerifyCsrfToken becaouse is being used on my producction laravel app, so i need a way to make this work.
I hope I was clear enough and thanks for your help
Please or to participate in this conversation.