php - 成功认证后 Laravel 社交名流登录不重定向
问题描述
我有一个使用 google 登录的 laravel 项目。我正在使用来自 laravel 的社交名流包。在他们在谷歌身份验证屏幕中确认后,我可以将用户的信息保存在我的数据库中,但之后它将始终重定向到登录。似乎Auth::login($user)
不起作用。我错过了什么?
这是我的登录控制器
public function redirectToProvider()
{
return Socialite::driver('google')->redirect();
}
public function handleProviderCallback()
{
try {
$user = Socialite::driver('google')->user();
} catch (\Exception $e) {
return redirect('/login');
}
$existingUser = User::where('email', $user->email)->first();
if($existingUser){
//login the user
\Auth::login($existingUser,true);
return redirect('/home');
} else {
// create a new user
$newUser = new User;
$newUser->name = $user->name;
$newUser->email = $user->email;
$newUser->google_id = $user->id;
$newUser->avatar = $user->avatar;
$newUser->avatar_original = $user->avatar_original;
$newUser->save();
\Auth::login($newUser,true);
}
return redirect('/home');
}
路线
Route::get('/redirect', 'Auth\LoginController@redirectToProvider');
Route::get('/callback', 'Auth\LoginController@handleProviderCallback');
来自谷歌控制台的授权重定向 URI
http://localhost:8000/callback
http://localhost:8000/home
使用社交名流在 laravel 中使用 google 登录参考:链接在这里
解决方案
我只是通过domain
在我的 session.php 中设置 null 来解决它。我不知道这是否是正确的答案,但我现在可以在使用谷歌登录后重定向到我的主页。也许如果我将它部署到生产中,我会将其更改domain
为我的服务器的实际 url。
/*
|--------------------------------------------------------------------------
| Session Cookie Domain
|--------------------------------------------------------------------------
|
| Here you may change the domain of the cookie used to identify a session
| in your application. This will determine which domains the cookie is
| available to in your application. A sensible default has been set.
|
*/
'domain' => null,
/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------
|
| By setting this option to true, session cookies will only be sent back
| to the server if the browser has a HTTPS connection. This will keep
| the cookie from being sent to you if it can not be done securely.
|
*/
'secure' => false,
推荐阅读
- mysql - 我可以在 MySQL 中将多个列批量设置为 NULL 吗?
- python - Python遍历唯一的交换方程
- java - 如何在antlr中输入代码行号到parseTree
- javascript - 列表类处于活动状态时如何更改特定图像
- symfony - Symfony 4 Security - 如何配置自定义 Guard 身份验证器?
- spring-boot - Cron 任务计划仅运行一次,但每 30 分钟运行一次
- python - 网页爬虫无法点击分页按钮
- css - GitHub 页面未更新 CSS
- python - 使用 pacman 在 Python 3 的 MSYS2 实例上下载 pyodbc
- reactjs - 我正在尝试动态创建 react.vis LineSeries 但 LineSeries 插入之后并且是空的