Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 349
Rob Avatar asked Mar 13 '16 04:03

Rob


People also ask

What is the use of Socialite in laravel?

Laravel Socialite is a package developed to abstract away any social authentication complexities and boilerplate code into a fluent and expressive interface. Socialite only supports Google, Facebook, Twitter, Linked In, Github, and Bitbucket as OAuth providers.

Can we create API in laravel?

Laravel lets you easily and quickly build RESTful APIs. This could be the back-end to a front-end web app, a data source for a mobile app, or a service for other apps or APIs. There are a lot of moving pieces to coding a RESTful API, but Laravel makes it a lot easier.

Does laravel use OAuth?

Laravel Sanctum does not support OAuth2; however, it provides a much simpler API authentication development experience.


1 Answers

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.

like image 142
Rob Avatar answered Sep 18 '22 18:09

Rob