senty
senty

Reputation: 12847

Laravel 5.1 - Socialite - Fetching User

Using official Laravel Docs 5.1 - Socialite, I set my routed and edited my AuthController.

   public function redirectToProvider()
   {  
      return Socialite::driver('facebook')
         ->scopes(['public_profile', 'email'])->redirect();
   }

It works perfectly and returns http://domain.dev/auth/facebook/callback?code=AQDLFlYr...

Then I used Laracasts' Socialite - Laravel 5.0, I tried turn my one into a proper Facebook authentication. I did everything until (min. 12.11, specifically) in the video. (At 12.11 he recaps the stuff in 15 sec). This is what I am trying to do.


Now, when I change my AuthController to:

  public function redirectToProvider(AuthenticateUser $authenticateUser, Request $request)
  {  
     return $authenticateUser->execute($request->has('code'));

  } 

...and have my AuthenticateUser class like this:

use Illuminate\Contracts\Auth\Guard;  (PS. I changed Authenticator to Guard)

  class AuthenticateUser {
       private $users;
       private $socialite;
       private $auth;

    public function __construct(UserRepository $users, Socialite $socialite, Guard $auth)
    {

      $this->users = $users;
      $this->socialite = $socialite;
      $this->auth = $auth;
    }

    public function execute($hasCode)
    {   
      // dd($hasCode) *First dd

      if ( ! $hasCode) return $this->getAuthorizationFirst();

      $user = $this->socialite->driver('facebook')->user();

      // dd($hasCode) *Second dd
      // dd($user) *Third dd
    }

    private function getAuthorizationFirst()
    {
      return $this->socialite->driver('facebook')
         ->scopes(['public_profile', 'email'])->redirect();
    } 
  }

*UserRepository is currently empty.


When I use *First dd, I receive False on the screen.

When I use *Second dd, I receive True.

When I use *Third dd, I receive nothing.

In all of these instances, now, I am receiving http://domain.dev/auth/facebook?code=9329409329042.


Edit:

.. and link includes ?code=9943290...

Upvotes: 0

Views: 676

Answers (1)

whoacowboy
whoacowboy

Reputation: 7447

It looks like you might have forgot the return.

return $authenticateUser->execute($request->has('code'));

enter image description here

Upvotes: 1

Related Questions