Laravel 6
There are 2 possible scenario:
Let's talk about the first scenario
In my routes/web.php, no middleware
Route::get('connect/{provider}', [ConnectController::class, 'connect'])
->name('connect');
Route::get('connect/{provider}/callback', [ConnectController::class, 'callback'])
->name('connect.callback');
In ConnectController.php
class ConnectController extends Controller
{
public function connect(Request $request, $provider)
{
$scopes = config('services.google.scopes');
// dump(auth('customer')->user()); <------- this 1 has value
return Socialite::driver($provider)
->scopes($scopes)
->redirect();
}
public function callback(Request $request, $provider)
{
$oauthUser = Socialite::driver($provider)->stateless()->user();
$user = auth('customer')->user();
debugbar()->log('callback user: ' . ($user->name ?? 'null'));
// ...
if (!auth('customer')->check()) {
debugbar()->log('user not logged in, log in now: ' . $u->name);
auth('customer')->login($u);
}
return redirect()->route('accounts');
}
Then the debugbar output is
log callback user: null
Suppose the user is logged in, and try to connect with google, but when reached the callback, the user session gone. What am I missing?
P/S: The default auth driver is admin
, cannot be changed.
After a day of research, I found out that the session ID is different from before navigate out to Google, and in the callback request.
Just update config/session.php, and set to lax
(originally was strict
)
[
// ...
'same_site' => 'lax',
]