Search code examples
phplaravellaravel-socialite

Using Laravel Socialite with an API?


I'm trying to use Laravel Socialite package over an api. I try to pass the code into my api to fetch the user but it keeps giving me an error:

Fatal error: Call to a member function pull() on null

Since I'm doing the request over an API, I take the following steps.

Send a request to api for the url to fetch the code:

Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()

Then make a request with the above fetched url, which redirects with the code parameter.

Send the code to the api and fetch the user:

$fb_user = Socialite::with('facebook')->user();

This is where it crashes. I'm not sure why.

I've used the package before and it works fine when I just have an app that reloads the page. But when I send it to an api (on a different domain) it crashes. I'm thinking there is some issue with how the code is generated. Is there anyway to fix this?


Solution

  • Just found my answer. Need to use stateless in both calls:

    Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()
    
    $fb_user = Socialite::with('facebook')->stateless()->user();
    

    Hope this helps someone.