2

I'm using laravel 5.8 in E-Commerce application

I need users to add products to cart without login from mobile application, and sync cart data with web after logging-in. so when user logging in from web account he can see his cart items added from mobile application.

I'm using database driver for session

MY api gaurd in kernel.php

       'api' => [
        'throttle:60,1',
        'bindings',
        StartSession::class,
        'session.exists',
    ],

checkExistsSession Middleware

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);
}

every thing is working good with postman and insomnia but when using real mobile devices it's creating a new session with each request until user logging in and UUID is not saved in sessions table.

this code is not working

DB::table('sessions')->where('id', $session->getId())->update(['uuid' => $uuid]);
Nikaido
  • 4,443
  • 5
  • 30
  • 47
Ahmed
  • 558
  • 1
  • 4
  • 22
  • After you login, the session ID changes. You need to work with a cookie and store an ID in there which you can later match in a new table. – Chris Oct 05 '19 at 18:03

0 Answers0