Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cizario's avatar

Socialite with Ajax

hi, i'm building "one single page" app (laravel v5.4.30) and i'm combining Socialite with Ajax to retrieve user's data from Linedin and Medium and i'm using laravel-cors to handle CORS but it seems not working, i tried many ways but still stuck.

i'm showing you screenshot to get things more clear : https://imgur.com/a/cee38

and this is my code config/cors.php

    'supportsCredentials' => false,
    'allowedOrigins' => ['http://ymapp.local'],
    'allowedOriginsPatterns' => [],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'],
    'exposedHeaders' => [],
    'maxAge' => 0,

kernel.php

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,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

    'cors' => \Barryvdh\Cors\HandleCors::class,  // Larvel CORS
];

web.php

Route::group(['middleware' => ['web']], function () {
[...]
    Route::get('/auth/{provider}',             ['as' => 'auth.provider',          'uses' => '\App\Http\Controllers\Front\AuthProviderController@redirectToProvider'])
        ->where(['provider' => 'linkedin|medium'])
        ->middleware('cors')
        ;

    Route::get('/auth/{provider}/callback',    ['as' => 'auth.provider.callback', 'uses' => '\App\Http\Controllers\Front\AuthProviderController@handleProviderCallback'])
        ->where(['provider' => 'linkedin|medium'])
        ->middleware('cors')
        ;
});

AuthProviderController.php

class AuthProviderController extends Controller 
{
    /**
     * Redirect the user to the GitHub authentication page.
     *
     * @return \Illuminate\Http\Response
     */
    public function redirectToProvider($provider)
    {
        return \Socialite::with($provider)->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @return \Illuminate\Http\Response
     */
    public function handleProviderCallback($provider)
    {
        $user = \Socialite::driver($provider)->user();
        // dd($user);
        return response()->json(['user' => $user]);
    }   
}

main.js

$('.auth-provider').click(function(e) {
    e.preventDefault();
   
   var $provider = $(this);
   
   $.ajaxSetup({
       cache: false,
       crossDomain: true,
       crossOrigin: true,
   });
  
  $.ajax({
      url: $provider.data('url'),
      dataType: 'json',
      success: function(json) {
         console.log(json);
      },
      error: function(json) {
        console.log(json);
     }
    
  });

});

i really appreciate any help

0 likes
1 reply
cizario's avatar

i'm showing you the error, if this could help

LINKEDIN_REDIRECT_URI=http://myapp.local/auth/linkedin/callback

OPTIONS https://www.linkedin.com/oauth/v2/authorization?client_id=[LINKEDIN_KEY]&redirect_uri=[LINKEDIN_REDIRECT_URI]&scope=r_basicprofile+r_emailaddress&response_type=code&state=yonkpoLjoWhB3mmBUlC8b5aKPNKHgUDSbx6CbeuT 404 ()
Failed to load https://www.linkedin.com/oauth/v2/authorization?client_id=[LINKEDIN_KEY]&redirect_uri=LINKEDIN_REDIRECT_URI]&scope=r_basicprofile+r_emailaddress&response_type=code&state=yonkpoLjoWhB3mmBUlC8b5aKPNKHgUDSbx6CbeuT: 

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myapp.local' is therefore not allowed access. The response had HTTP status code 404. 

when i paste and go the url (console)

https://www.linkedin.com/oauth/v2/authorization?client_id=[LINKEDIN_KEY]&redirect_uri=[LINKEDIN_REDIRECT_URI]&scope=r_basicprofile+r_emailaddress&response_type=code&state=yonkpoLjoWhB3mmBUlC8b5aKPNKHgUDSbx6CbeuT

i got the user data from linkedin the same with medium, so what i'm missing ? i'm sur i'm overlooking something.

Please or to participate in this conversation.