as of yesterday, the code below worked. today, however, i had to run a php artisan config:cache command in laravel as i added a package and now my nice ionic app does not want to run connect to anything as i keep getting this 403 error.
the error started after i installed "rap2hpoutre/laravel-log-viewer": "^0.19.1", and cached, but i don't think that has anything to do with it. i was pretty sure my cache was up to date prior to that.
jwt produces the same error.
previously, the app was working without a cors plugin.
it provides me this error locally and on my server (as i had to cache there too).
this error is different than previous errors i have gotten when debugging this.
when i pulled the route http://xxx/api/home normally in chrome - it returns fine... same in postman
Thanks for your help!
THE ERROR
OPTIONS http://xxx/api/home 403 (Forbidden) Failed to load http://xxx/api/home: Response for preflight does not have HTTP ok status.
IONIC
basicGet_no_token(rl){
console.log('basicGet - ' + this.base_url + rl);
return new Promise((resolve, reject) => {
this.http.get(this.base_url + rl,
{headers: new HttpHeaders({
'Content': 'application/json',
'Accept': 'application/json',
})})
.subscribe(res => {console.log(res);resolve(res);}, (err) => {this.handleError(err);console.log(err);reject(err);});
});
}
LARAVEL
Route::group(['middleware' => ['addHeaders']], function () {
Route::get('home', 'api\WelcomeController@home');
});
class addHeaders
{
public function handle($request, Closure $next)
{
if ($request->getMethod() == "OPTIONS") {
return response(['OK'], 200)
->withHeaders([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Headers' => 'Origin, Content-Type, X-Auth-Token, authorization, X-Requested-With'
]);
}
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
->header('Access-Control-Allow-Credentials', true)
->header('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token, authorization, X-Requested-With');
}
}
class WelcomeController extends Controller
{
public function home()
{
$r['message']="Welcome!";
$r['allow']=true;
$p=compact('r');
return response()->json($p, 200);
}
}
class Kernel extends HttpKernel
{
protected $middlewareGroups = [
'api' => [
'throttle:60,1',
'bindings',
],
];
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'jwt' => \App\Http\Middleware\JWT::class,
'addHeaders' => \App\Http\Middleware\addHeaders::class,
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
}