commerce
commerce copied to clipboard
[BUG]logout not working in demo site
Hi,
I noticed in my deployed site that if a customer clicks log out, and then refreshes the page, he gets login again. Also, I Confirmed the same on the Demo site and saw the same bug present over there also. It's only happening in Deployed sites, on the local cookie is getting deleted fine. Is everyone else seeing the same in their installations?
I can't reproduce this bug 🤔 . Are you on the bigcommerce demo or the shopify demo?
Hi, Thanks for the reply. I am working with bigcommerce. Here is the URL [https://demo.vercel.store/]. Please try to replicate here.
Hi, @lfades Do we have any updates on this bug. or any workaround, for now, would be much help full.
@ahmadsyed I'll take a look tomorrow 👍
I tried to fix the Commerce code, but it's a little too abstracted for me to follow. For whatever reason the function returned from useLogout does not appear to be touching the SHOP_TOKEN cookie at all. I ended up creating a new API route in Next using similar logic:
// pages/api/customers/logout.ts
// this has to come from the server/API side as it's an HTTP only cookie
import { serialize } from 'cookie'
export default async function handler(_, res) {
res.setHeader(
'Set-Cookie',
serialize('SHOP_TOKEN', '', { expires: new Date(0), path: '/' })
)
res.setHeader(
'Set-Cookie',
serialize('Shopper-Pref', '', { expires: new Date(0), path: '/' })
)
return res.status(200).json({ message: 'logged out successfully' })
}
Then I hit this from the client-side when I need to logout.
import { useRouter } from 'next/router'
const router = useRouter()
...
return (
<button
onClick={async () => {
await fetch('/api/customers/logout')
router.reload()
}}
>Logout</button>
)
Hope this helps future readers while the Vercel team figures something out with the Commerce API.
I don't know why, but I think this problem is related to the HTTP method. I modified from GET to POST and it worked.
Hey there! Thank you for your contribution. We have decided to take Next.js Commerce in a new direction and will be closing out current PRs and issues due to this change. Please see this PR for more details: #966