dongxin8392 2018-09-19 06:01
浏览 50

Laravel 5.6与社交名媛登录facebook

i try to make login with facebook use socialite. I had register on developers.facebook.com and got my APP_ID and Secret_code and i had implement it in my .env file and config/services.php. But when after i input my username and password facebook it can't direct me to homepage. i used authentication scaffolding from laravel

Error

This site can't be reach

LoginController.php

<?php

 namespace App\Http\Controllers\Auth;
 use App\User;
 use Socialite;
 use Auth;
 use App\Http\Controllers\Controller;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;


 class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest')->except('logout');
}

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

/**
 * Obtain the user information from provider.  Check if the user already exists in our
 * database by looking up their provider_id in the database.
 * If the user exists, log them in. Otherwise, create a new user then log them in. After that
 * redirect them to the authenticated users homepage.
 *
 * @return Response
 */
public function handleProviderCallback($provider)
{
    $user = Socialite::driver($provider)->user();
    $authUser = $this->findOrCreateUser($user, $provider);
    Auth::login($authUser, true);
    return redirect($this->redirectTo);
}

/**
 * If a user has registered before using social auth, return the user
 * else, create a new user object.
 * @param  $user Socialite user object
 * @param $provider Social auth provider
 * @return  User
 */
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
    ]);
}
  }

.env

FB_ID=321**14684*****
FB_SECRET=40fb4782*****5b7b88391f4599f18c7
FB_URL=https://localhost:8000/auth/facebook/callback

web.php

Route::get('/', function () {
return view('homepage');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('auth/{provider}', 'Auth\LoginController@redirectToProvider');
Route::get('auth/{provider}/callback', 
'Auth\LoginController@handleProviderCallback');

**App\User.php**
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

 class User extends Authenticatable
 {
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password','provider','provider_id'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
 }

Config/services.php

        'facebook' => [
        'client_id'     => env('FB_ID'),
        'client_secret' => env('FB_SECRET'),
        'redirect'      => env('FB_URL'),
        ],

Please give me some advice to solve this problem. I'm sorry for my bad English. Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭