0

Problem

When I try to login with Facebook (using Laravel Socialite) it returns wrong callback route, and throws a NotFoundHttpException.

/callback?code=xxxxxxxxxxxxxxxxx

I think it should return callback/facebook?code=xxxxxx for the login to work.

Route.php

Route::get('/callback/{provider}', 'SocialAuthController@callback');
Route::get('/redirect/{provider}', 'SocialAuthController@redirect');

Service.php

'facebook' => [
    'client_id'     => env('FB_CLIENT_ID_DEV'),
    'client_secret' => env('FB_CLIENT_SECRET_DEV'),
    'redirect'      => env('FB_REDIRECT_DEV'),
],

.ENV

FB_CLIENT_ID_DEV        = 'xxxxxxxxxxx'
FB_CLIENT_SECRET_DEV    = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
FB_REDIRECT_DEV         = 'http://localhost:8000/callback/facebook'

SocialAuthController

public function redirect($provider)
{
    return Socialite::driver($provider)->redirect();
}

public function callback(SocialAccountService $service, $provider)
{
    // Important change from previous post is that I'm now passing
    // whole driver, not only the user. So no more ->user() part
    $user = $service->createOrGetUser(Socialite::driver($provider));

    auth()->login($user);

    return redirect()->to('index');
}

SocialAccountService

class SocialAccountService
{
    public function createOrGetUser(Provider $provider)
    {

        $providerUser = $provider->user();
        $providerName = class_basename($provider);

        $account = SocialAccount::whereProvider($providerName)
            ->whereProviderUserId($providerUser->getId())
            ->first();

        if ($account) {
            return $account->user;
        } else {

            $account = new SocialAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => $providerName
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();

            if (!$user) {

                $user = User::create([
                    'email'         => $providerUser->getEmail(),
                    'name'          => $providerUser->getName(),
                    'status'        => 1,
                    'description'   => "Saknas."
                ]);
            }

            $account->user()->associate($user);
            $account->save();

            return $user;
        }
    }
}

Note

I am following this tutorial: https://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-twitter-login.html#.WAZGQ-CLSUn . The Socialite login with Google works, but not Facebook.

Olof84
  • 919
  • 4
  • 14
  • 29
  • 2
    check if `env('FB_REDIRECT_DEV')` is returning the right value. – Neal Oct 18 '16 at 16:46
  • Hi and thanks for commenting. I checked, and it does. – Olof84 Oct 18 '16 at 16:51
  • it might sound silly but try changing it to `/facebook/callback` instead of `/callback/facebook` also in your route. – Neal Oct 18 '16 at 16:54
  • Thanks, maybe I try it later. Though you might have right about the FB_REDIRECT_DEV. When I echo it out from my controller it says it is http://localhost:8000/callback. Something might be weird with the .env file. – Olof84 Oct 18 '16 at 17:25
  • 1
    Thanks Neal, you where right, FB_REDIRECT_DEV was not updated. I fixed it by running php artisan config:cache the .env. If you write your comment as an answer I can set it as the answer. – Olof84 Oct 18 '16 at 17:44
  • Glad that I helped directing you to the solution. Wish me luck man, i going through some tuff decisions in my life :D – Neal Oct 19 '16 at 09:00

0 Answers0