I have always stored a UID in the cookie so I can identify the user during their session, and used that UID in my database for tracking items in the cart within the database.
Once the user logs in, I update the users record in the database to add their sessions UID while still tracking their session with cookies.
I am yet to use localStorage myself so can't comment on whether that would be better then using cookies or not.