Laravel使用社交网站进行身份验证和路由

时间:2016-03-19 04:26:31

标签: laravel laravel-5 laravel-5.1 laravel-routing laravel-socialite

有人请帮助我。我正在尝试实现社交网络身份验证,一切正常,但Auth下的路由不起作用。他们重定向回登录页面。我的Auth或路由有什么问题。我的路由和控制器文件位于

之下
Route::group(['middleware' => ['web']], function () {
    Route::get('facebook',array( 
        'as' => 'facebook', 
        'uses' =>'FacebookController@redirectToProvider'
    ));
    Route::get('Callback', 'FacebookController@handleProviderCallback');
}); 
Route::group(['middleware' => ['web','auth']], function () {    
    Route::get('play', function () {
        return view('landing');
    });

    Route::get('go', function () {
        return view('landing');
    });
});

控制器

public function handleProviderCallback()
{
    try 
    {
        $user = Socialize::with('facebook')->user();
    } 
    catch (Exception $e) 
    {
        return Redirect::to('login');
    }

    $authUser = $this->findOrCreateUser($user);
    Auth::login($authUser, true);
    $user = Auth::user();
        return Redirect::to('play');       
}

    private function findOrCreateUser($facebookUser)
    {
        if ($authUser = User::where('id', $facebookUser->getId())->first()) 
        {
            return $authUser;
        }
        return User::create([
            'id' => $facebookUser->getId(),
            'name' => $facebookUser->getName(),
            'email' => $facebookUser->getEmail(),
            'gender' => $facebookUser->user['gender'],
            'avatar' => $facebookUser->getAvatar(),
            'avatar_original' => $facebookUser->avatar_original,
            'verified' => $facebookUser->user['verified']
        ]);
    }

0 个答案:

没有答案