在Laravel中使用社交名流登录Facebook

时间:2019-01-28 07:43:01

标签: laravel facebook laravel-socialite

按照laravel中的social lite使用facebook Oauth的步骤

https://itsolutionstuff.com/post/laravel-56-login-with-facebook-with-socialiteexample.html

但是我设置好之后,出现了ERR_TOO_MANY_REDIRECTS之类的问题。

这是重定向我的链接:

https://www.facebook.com/v3.0/dialog/oauth?client_id=xxxxxxxxxxxx&redirect_uri=http%3A%2F%2Flocalhost%2Ffesbok%2Fpublic%2Fauth%2Ffacebook%2Fcallback&scope=email&response_type=code&state=0R4djYkaHGFEHcXF8CDZQEC52TGvzA82jr77PPp5#=

可能是什么问题?我相信一切都遵循了。请让我知道我错过了什么。

1 个答案:

答案 0 :(得分:0)

这是我在PROD中用于辅助项目的代码。到目前为止,没有任何问题。

我希望它可以帮助您完成棘手的事情:D,因为您可以看到它比看起来容易。

错误可能是您设置处理Auth和Callback的路由的方式,对我来说,您似乎具有相同的路由,并且在Facebook批准并向用户发送回网站时也是如此再次重定向到OAuth端点,依此类推。

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Response;
use Laravel\Socialite\Facades\Socialite;

class LoginController extends Controller
{
    /**
     * Redirect the user to the GitHub authentication page.
     *
     * @param $provider
     * @return Response
     */
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @param $provider
     * @return Response
     */
    public function handleProviderCallback($provider)
    {
        $user = Socialite::driver($provider)->user();

        $authUser = $this->findOrCreateUser($user, $provider);

        auth()->login($authUser, true);

        return redirect('/home');
    }

    /**
     * Finds or creates an user.
     *
     * @param $user
     * @param $provider
     * @return mixed
     */
    public function findOrCreateUser($user, $provider)
    {
        $authUser = User::where('provider_id', $user->id)->first();
        if ($authUser) {
            return $authUser;
        }

        return User::create([
          'name'     => $user->name,
          'email'    => $user->email,
          'provider' => $provider,
          'provider_id' => $user->id,
      ]);
    }
}

这就是我们设置服务的方式(代替环境,您还可以使用路由功能。)

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third party services such
    | as Stripe, Mailgun, SparkPost and others. This file provides a sane
    | default location for this type of information, allowing packages
    | to have a conventional place to find your various credentials.
    |
    */

    'twitter' => [
       'client_id' => env('TWITTER_CLIENT_ID', ''),
       'client_secret' => env('TWITTER_CLIENT_SECRET', ''),
       'redirect' => env('APP_URL', '').'/auth/twitter/callback',
      ],

    'facebook' => [
       'client_id' => env('FACEBOOK_CLIENT_ID', ''),
       'client_secret' => env('FACEBOOK_CLIENT_SECRET', ''),
       'redirect' => env('APP_URL', '').'/auth/facebook/callback',
      ],

    'google' => [
       'client_id' => env('GOOGLE_CLIENT_ID', ''),
       'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
       'redirect' => env('APP_URL', '').'/auth/google/callback',
      ],
];
//Routes for socialite
Route::get('auth/{provider}', 'LoginController@redirectToProvider');
Route::get('auth/{provider}/callback', 'LoginController@handleProviderCallback');