gpt4 book ai didi

php - 使用 Laravel 5.1 登录 Facebook Socialite

转载 作者:可可西里 更新时间:2023-11-01 13:27:48 25 4
gpt4 key购买 nike

我已按照 http://laravel.com/docs/5.1/authentication 中的所有步骤进行操作在 Laravel 5.1 上使用 facebook 进行社交登录。

这里是我遵循的步骤:

1 - 在我的 Laravel 根项目中使用命令:

composer require laravel/socialite

2 - 在你的 config/app.php 配置文件中注册 Laravel\Socialite\SocialiteServiceProvider:

'providers' => [
// Other service providers...

Laravel\Socialite\SocialiteServiceProvider::class,
],

3 - 将 Socialite facade 添加到应用配置文件中的别名数组:

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

4 - 在 config/services.php 中添加 facebook 服务:

 'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => 'http://homestead.app/auth/facebook/callback',
],

5 - 将方法 redirectToProvider() 和 handleProviderCallback() 添加到我的 AuthController.php 中:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Socialite;

class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

/**
* Redirect path after successful login
*
* @var string
*/
protected $redirectPath = '/greeting';

/**
* Redirect path after failed login
*
* @var string
*/
protected $loginPath = '/auth/login';

/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}

/**
* Redirect the user to the Facebook authentication page.
*
* @return Response
*/


public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}

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

dd($user);
}
}

我想强调一下,我已经在文件夹 app/Http/Controllers/Auth/中创建了 AuthController.php,因为在它要求创建路由之后:'Auth\AuthController@redirectToProvider''Auth\AuthController@handleProviderCallback'

6 - 我已经添加了路线:

Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider');
Route::get('auth/facebook/callback', 'Auth\AuthController@handleProviderCallback');

现在,当我在登录页面中单击“使用 facebook 登录”时,它会转到 http://homestead.app/auth/facebook并给我以下错误:

FatalErrorException in AuthController.php line 90: Class 'Socialite' not found

我尝试关注代码 anchor guide但它适用于 laravel 5.0(我需要在 laravel 5.1 上使用它)

最佳答案

最后我通过删除所有内容并从头开始重新安装 Homestead 解决了问题

关于php - 使用 Laravel 5.1 登录 Facebook Socialite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32550110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com