Laravel does not save my session after auth()->login()
Hello. I'm using Socialite to create Google login in Laravel application, the redirect and callback functions worked fine. But I am having some trouble in login session, Laravel did not save my session after calling Auth::login(), and Auth::user() returns null, but Auth::user() still works in the function (controller action) where Auth::login() called. Can someone help me?
class LoginController extends Controller
{
public function callback(Request $request) {
$googleUser = Socialite::driver('google')->user();
$userLogin = [
'id' => $googleUser->getId(),
'name' => $googleUser->getName(),
'email' => $googleUser->getEmail(),
'avatar' => $googleUser->getAvatar(),
];
$email = $userLogin['email'];
$user = User::updateOrCreate(
['email' => $email],
$userLogin
);
auth()->login($user);
// This still working.
// dd(auth()->user());
return redirect()->route('debug');
}
}
But it wasn't work in another controller action
class DebugController extends Controller {
public function __invoke() {
$user = auth()->user();
// This returns null
dd($user);
}
}