我今天已从5.1迁移到5.2,并希望验证能够正常运行。但它并没有坚持登录本身。我使用Googles OAuth 2.0并在数据库中保存id和名称。
// OAuth flow..
Auth::loginUsingId($google_plus_id);
return redirect('/debug');
在调试路线上,我只有一个dd(auth()->user())
但是在重定向之后,这总是返回null。
当我登录及之后dd()
经过身份验证的用户时,它会起作用
但是重定向到另一条路线并且它是NULL
我知道新的中间件组网络,因此我的routes.php看起来像这样:
Route::group([ 'middleware' => [ 'web' ] ], function () {
Route::get('/auth/google', [
'as' => 'google.login',
'uses' => 'Auth\OAuthController@getGoogleRedirect'
]);
Route::get('/auth/google/callback', [
'as' => 'google.callback',
'uses' => 'Auth\OAuthController@getGoogleCallback'
]);
Route::get('/debug', function(){
dd(auth()->user());
});
});
也调整了auth.php配置文件。
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Http\Models\GooglePlusUser::class,
],
],
GooglePlusUser类正在使用可验证的特征。整个设置在L5.1中运行良好但不知何故它在5.2中乱七八糟,我似乎无法找到原因。
答案 0 :(得分:1)
不知道为什么会发生这种情况,也许基本的Eloquent模型本身已经改变,但它将标识符视为整数,从而导致整数的最大值。但是,google的id长度为21个字符,因此在数据库中保存为字符串,而不是int。在GooglePlusUser类中应用mutator来进行字符串化解决了它。
编辑:似乎在Eloquent模型中的第2690行引用了这个函数:
public function getCasts()
{
if ($this->incrementing) {
return array_merge([
$this->getKeyName() => 'int',
], $this->casts);
}
return $this->casts;
}
编辑2:比使用mutator更简单的方法是使用protected $casts = [ 'id' => 'string' ];
Laravel Docs