我laravel 5.8
在电子商务应用程序中使用
我需要用户在不从移动应用程序登录的情况下将产品添加到购物车,并在登录后将购物车数据与网络同步。因此,当用户从网络帐户登录时,他可以看到从移动应用程序添加的购物车项目。
我正在使用数据库驱动程序进行会话
我的 api 守卫在kernel.php
'api' => [
'throttle:60,1',
'bindings',
StartSession::class,
'session.exists',
],
checkExistsSession 中间件
public function handle($request, Closure $next)
{
$user = Auth::user();
$uuid = $request->header('UUID', 0);
$session = $request->session();
if (($user || $uuid) && config('session.driver') == 'database') {
$exists = DB::table('sessions')
->where(function ($q) use ($uuid, $user) {
if ($user) {
$q->where('user_id', $user->id);
}
$q->orWhere('uuid', $uuid);
})
->where('id', '!=', $session->getId())
->first();
if ($exists) {
// set the previous session as current session
$session->setId($exists->id);
}
}
if ($uuid) {
DB::table('sessions')->where('id', $session->getId())->update(['uuid' => $uuid]);
}
return $next($request);
}
每件事都适用于邮递员和失眠症,但是当使用真正的移动设备时,它会为每个请求创建一个新会话,直到用户登录并且UUID
不会保存在会话表中。
此代码不起作用
DB::table('sessions')->where('id', $session->getId())->update(['uuid' => $uuid]);