0

I created an API with Laravel and I try to use it with an Ionic 2 project

I'm trying to make a GET request to my Laravel API.

When I make my http request to

auth/{prodiver}/callback/{userID}/{accessToken}

I got this error:

Failed to load http://api.url.net/api/auth/facebook/callback/XXXX/MY_TOKEN: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8001' is therefore not allowed access.

My route looks like this:

Route::group(['middleware' => ['guest', 'cors']], function() {
    Route::get('test', function () {
        return response()->json('{dummy.data}', 200);
    });

    Route::get('auth/{prodiver}/callback/{userID}/{accessToken}', 'SocialAuthController@callback')->middleware('cors')->name('social.auth'); // social auth
});

But when I try the same code, with the route test, it works and I got the data...

The "facebook connect" isn't the problem, the success callback is triggered.

Here is the callback() method

public function callback($provider, $userID, $accessToken)
{
    if ($provider == "facebook"){
        $user = $this->createOrGetUser($userID, $accessToken);
        auth()->login($user);
        return response()->json($user, 200);
    }
}

All other http requests works perfectly...

Here is the concerned http request (in Ionic project)

let headers = new Headers(); 
headers.append('Content-Type', 'application/json'); 
let url = this.baseApiUrl+'auth/facebook/callback/' + userID + '/' + accessToken; 
this.http
    .get(url, {headers: headers})
    .map(res => res.json())
    .subscribe(data => {
            console.log(data)
        }, err => {
            this.onError(err);
        }
    );

I don't understand what's going on, anyone has a clue ? I'm totally tired of this error :/

Thanks a lot! :)

Doddo
  • 51
  • 1
  • 1
  • 3
  • You've mentioned `cors` middleware. What package do you use, `barryvdh/laravel-cors`? Did you configure it properly? – shukshin.ivan Aug 29 '17 at 23:58
  • Yes it is, I let the config to "*" everywhere, for developement... – Doddo Aug 30 '17 at 00:28
  • 1
    Possible duplicate of [No 'Access-Control-Allow-Origin' header - Laravel 5.4](https://stackoverflow.com/questions/43565877/no-access-control-allow-origin-header-laravel-5-4) – Demonyowh Aug 30 '17 at 01:18
  • You can try browser plugin Cross-Origin Resource Sharing(CORS). I was also facing this problem ionic + laravel 5.2. It's work for me. – rahul patel Aug 30 '17 at 10:22
  • Yup, I can pass a CORS browser plugin, but isn't it a little bit stupid? It's for an Ionic app, and I can't ask the users to install it... – Doddo Sep 01 '17 at 08:32

1 Answers1

1

First, why do you set cors middleware when defining the second route? It is already set for the whole group.

Second, cors returns a cors error in case you have some error in your code, that's its behaviour.

When an error occurs, the middleware isn't run completely. So when this happens, you won't see the actual result, but will get a CORS error instead.

Try to debug your code to find a bug. Test returns data, it means that this is not the cors who gives the error, it is your code.

shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
  • Hi, thanks for your answer. I put CORS middleware on route and group, just to try, to be sure the middleware is "active"... I'll check my code to find an error, thanks :) – Doddo Sep 01 '17 at 08:30
  • You where right about the middleware! There was an error in my script... But I got an other problem with the "preflight request" now... :/ – Doddo Sep 01 '17 at 08:48