5

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,
    ];
}
Pete
  • 312
  • 2
  • 3
  • 15

4 Answers4

10

This answer may not suit all of you but if you created custom request and forget to authorize method return true you may encounter with this error.

I'm handling with authorization at policies and forget about the requests validation often. Double check when you create custom requests

Note: also note that if you are using policies, you may forgot the map your policy to model in your AuthServiceProvider

Teoman Tıngır
  • 2,766
  • 2
  • 21
  • 41
4

As stated in the beginning you are using caches - at least one of them for config.

Some packages do not play well with caching machanisms of Laravel and sometimes you just forget that you are using any caching - you should always keep that in mind!

Therefore don't use any caching in dev (this is my personal preference to not loose time for not existing problems).

And in production, while deploying, you need to make yourself absolutely sure that all caches will get recreated.

These functions might be handfull to you:

php artisan cache:clear

php artisan route:clear

php artisan config:clear

php artisan view:clear

So please check them out...

Bart
  • 1,889
  • 1
  • 21
  • 38
0

I was able to find a backup copy of my configuration and restore it.

Pete
  • 312
  • 2
  • 3
  • 15
0

Try to request to Secure URL like; HTTPS://xxxx.yy/endpoint

API generally expects requests with HTTPS. If you use POSTMAN you should be sure about it.

Enjoy your coding!

Enver
  • 542
  • 5
  • 7