Laravel5.1にてTwitterによるログイン機能を作っています。
やり方は下のサイトのソーシャル認証の部分の通りにやっています。
認証までは問題なさそうなのですが、認証後のリダイレクト先で
FatalErrorException in mypage.php line 3:
Class 'Socilite' not found
というエラーが出てしまいます。
service.phpでは、
php
1 'twitter' => [ 2 'client_id' => '#####', 3 'client_secret' => '#####', 4 'redirect' => url('mypage'), 5 ],
こんな感じでリダイレクト先を指定しています。
リダイレクト先のファイル(mypage.blade.php)
php
1<?php 2 3$user = Socialite::driver('twitter')->user(); 4 5echo $user; 6 7 8 9?>
認証済みユーザの情報を取得し、出力したいのですが、どこが間違っているのでしょうか?
#####追記######
ソーシャル認証のコントローラは以下のようになっています。
SocialauthController.php
php
1<?php 2 3namespace App\Http\Controllers\Auth; 4use Socialite; 5 6use App\User; 7use Validator; 8use App\Http\Controllers\Controller; 9use Illuminate\Foundation\Auth\ThrottlesLogins; 10use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 11 12 13 14class SocialauthController extends Controller 15{ 16 /* 17 |-------------------------------------------------------------------------- 18 | Registration & Login Controller 19 |-------------------------------------------------------------------------- 20 | 21 | This controller handles the registration of new users, as well as the 22 | authentication of existing users. By default, this controller uses 23 | a simple trait to add these behaviors. Why don't you explore it? 24 | 25 */ 26 27 use AuthenticatesAndRegistersUsers, ThrottlesLogins; 28 29 30 /** 31 * Create a new authentication controller instance. 32 * 33 * @return void 34 */ 35 public function __construct() 36 { 37 $this->middleware('guest', ['except' => 'getLogout']); 38 } 39 40 /** 41 * Get a validator for an incoming registration request. 42 * 43 * @param array $data 44 * @return \Illuminate\Contracts\Validation\Validator 45 */ 46 protected function validator(array $data) 47 { 48 return Validator::make($data, [ 49 'name' => 'required|max:255', 50 'email' => 'required|email|max:255|unique:users', 51 'password' => 'required|confirmed|min:6', 52 ]); 53 } 54 55 /** 56 * Create a new user instance after a valid registration. 57 * 58 * @param array $data 59 * @return User 60 */ 61 protected function create(array $data) 62 { 63 return User::create([ 64 'name' => $data['name'], 65 'email' => $data['email'], 66 'password' => bcrypt($data['password']), 67 ]); 68 } 69 70 /** 71 * ユーザーをGitHubの認証ページヘリダイレクト 72 * 73 * @return Response 74 */ 75 public function redirectToProvider() 76 { 77 return Socialite::driver('twitter')->redirect(); 78 } 79 80 /** 81 * ユーザーの情報をtwitterから取得 82 * 83 * @return Response 84 */ 85 public function handleProviderCallback() 86 { 87 $user = Socialite::driver('twitter')->user(); 88 89 // $user->token; 90 } 91} 92
ログインページにて認証が行われ、リダイレクトはされるのでSocialiteは機能しているような気がするのですが・・
また、公式のドキュメントでは、Socialiteを利用する際に、composer.jsonに記述を加えるような表現はないと思うのですが、composer.jsonに記述を追加する必要があるのでしょうか・・・・?
composer.jsonは以下の通りです。
composer.json
"require": { "laravel/socialite": "^2.0" }
mypage.blade.php内のfacadeがスペルもミスでした。
誤Socilite 正Socialite
しかし訂正後は以下のエラーが発生しました。
Object of class Laravel\Socialite\One\User could not be converted to string (View: /var/www/html/dev/lara/resources/views/mypage.blade.php)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。