在null上调用成员函数set()

时间:2015-12-24 06:03:11

标签: laravel-5

我试图在laravel 5中使用oauth for google,但我收到了错误消息。任何人都可以帮我解决这个问题。

以下是我的档案,请查看

.env

'google' => [
        'client_id' => env('GOOGLE_ID'),
        'client_secret' => env('GOOGLE_SECRET'),
        'redirect' => env('GOOGLE_REDIRECT'),
    ],

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

public function handleProviderCallback()    {
        $user = Socialite::driver('google')->user();
            console.log($user);
}

services.php

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

//I have set the providers and aliases in app.php.

routes.php文件

public function redirect()
{
    $state = str::random(40);

    if ($this->usesState()) {
        $this->request->getSession()->set('state', $state);
    }
    return new RedirectResponse($this->getAuthUrl($state));
}

以下是我收到错误的代码

//在set()方法

await tile.RequestCreateAsync();

提前致谢..

1 个答案:

答案 0 :(得分:10)

嘿如果您使用laravel 5.2,这对我有用。

将您的控制器置于'web'中间件中。喜欢,

Route::group(['middleware' => 'web'], function() {
   Route::get('google', 'Auth\AuthController@redirectToProvider');
   Route::get('google/callback', 'Auth\AuthController@handleProviderCallback');
});

并确保内核文件已注册中间件类。

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \Perkweb\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Perkweb\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];