Using socialite with an API?

Published 1 year ago by websanova

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.

  1. send a request to api for the url to fetch the code:

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

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

  3. 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?

Best Answer (As Selected By websanova)
websanova

Found out my issue, have to use stateless in both calls:

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

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

Found out my issue, have to use stateless in both calls:

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

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

@websanova I'm sorry I'm trying to do the exact same thing based on your idea but I can't understand what you mean by making a request with the fetched url (getTargetUrl()) in this case.

I'm using React JS and I'm not sure what to do with the target URL. I don't want to webpage to end up at just the token I've generated or the backend. Any help is appreciated. Thanks.

Also CC'ing @isaackearl because I read most of his stuff to get through to where I am now. Thanks!!

reiphoton

Eh lemme reply to my own question. I took a break, tunnel vision was locking my brain in.

First I made my callback the same as my frontend url so: http://localhost:3000/auth/google/callback/ for my Google Provider callback in my Laravel app/services.php. Don't forget to allow the URL(s) on the respective social login provider dev consoles and make sure to restart your sever.

Then on my React frontend, with the URL now redirecting with the code param as a variable which I parsed with my library of choice 'query-string' to get the code text/token.

Then I made an axios POST request to the backend with the code attached to the end of the url...

http://localhost:8000/social/auth/google/callback/code=gibBeriShStriNgYouGotHere and Socialite did the rest... With some help from https://isaacearl.com/blog/stateless-socialite to understand. After which I returned to my frontend a JWTAuth Token, just like with my regular email+password login. Yay!

andrewastone

@reiphoton Hello, thanks to replying to your reply with more information! I'm in the same boat (laravel backend + react fronted) trying to get social login implemented.

I have my callback url: http://localhost:8000/login/facebook/callback?code=gibBeriShStriNgYouGotHere

...which lands on the front-end, where a component parses the code param successfully and send it to a dispatcher containing the axios POST request.

axios.post(`http://localhost:8000/login/facebook/callback?code=${code}`);

This is where I'm having trouble... did you set up a new laravel POST route to a controller action? I'm not sure how you were able to return the Token, and how "Socialite did the rest".

I'd appreciate it so much if you can help me wrap my head around this as you were able to. Thanks!

robrogers3

if solved please close,

Please sign in or create an account to participate in this conversation.