gpt4 book ai didi

laravel-5 - 对 null 调用成员函数 set()

转载 作者:行者123 更新时间:2023-12-03 06:54:44 26 4
gpt4 key购买 nike

我正在尝试在 Laravel 5 中使用 oauth for google,但出现错误。谁能帮我解决这个问题。

以下是我的文件,请查看

.env

GOOGLE_ID = 'mygoogleId' 
GOOGLE_SECRET = 'mysecretID'
GOOGLE_REDIRECT = http://localhost:8090/users

services.php

'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);
}

routes.php

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

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

这是我收到错误的代码

//在set()方法上

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

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

提前致谢..

最佳答案

嘿,如果你使用的是 Laravel 5.2,这对我来说很有效。

将您的 Controller 放入“网络”中间件中。就像,

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',
],
];

关于laravel-5 - 对 null 调用成员函数 set(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34448050/

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