vercel / commerce

Next.js Commerce
https://demo.vercel.store
MIT License
11.53k stars 4.26k forks source link

Bigcommerce: Login has stopped working #299

Closed magicspon closed 2 years ago

magicspon commented 3 years ago

Login has suddenly stopped working.

Steps to reproduce

  1. Go to http://demo.vercel.store/... create an account.
  2. Open the network tab
  3. Login
  4. Modal disappears, as if log has been success.
  5. Look at the customer api call. Note the response is { data: null }

If you try logging in with the incorrect details, you get an error message, as expected.

I'm not sure where the issue has come from. My store has been working, I've made zero updates in the last 46 days. And now suddenly it's stopped working.

Any thoughts?

clockelliptic commented 3 years ago

They have never worked for me. I'm actually just circling back to this because I thought maybe I could make some headway here, but I'm stumped.

clockelliptic commented 3 years ago

I believe this may be a duplicate of https://github.com/vercel/commerce/issues/273

clockelliptic commented 3 years ago

I just solved the problem for myself. There is an additional cookie Shopper-Pref that needs to be set at login.

I changed the async function login(){...} definition in the file framework/bigcommerce/auth/login.ts (lines 32-71) to the following:

async function login({
  query = loginMutation,
  variables,
  res: response,
  config,
}: {
  query?: string
  variables: LoginVariables
  res: ServerResponse
  config?: BigcommerceConfig
}): Promise<LoginResult> {
  config = getConfig(config)

  const { data, res } = await config.fetch<RecursivePartial<LoginMutation>>(
    query,
    { variables }
  )

   // Set-Cookie returns several cookies, we want SHOP_TOKEN and Shopper-pref
   let shopToken = getCookie(res.headers.get('Set-Cookie'), 'SHOP_TOKEN');
   let prefToken = getCookie(res.headers.get('Set-Cookie'), 'Shopper-Pref');
   if (shopToken && typeof shopToken === 'string') {
     // In development, don't set a secure shopToken or the browser will ignore it
     if (process.env.NODE_ENV !== 'production') {
       shopToken = shopToken.replace(/; Secure/gi, '')
       shopToken = shopToken.replace(/; SameSite=none/gi, '; SameSite=lax')
     }
     response.setHeader(
       'Set-Cookie',
       concatHeader(response.getHeader('Set-Cookie'), shopToken)!
     )
     response.setHeader(
       'Set-Cookie',
       concatHeader(response.getHeader('Set-Cookie'), `${prefToken}`)!
     )
    }

    return {
      result: data.login?.result,
    }
}
magicspon commented 3 years ago

@clockelliptic Take a look at the fix here: https://github.com/bigcommerce/storefront-data-hooks/pull/62 you'll want to "Set the cookie at TLD to make it accessible on subdomains" You'll need this otherwise when a user goes to the checkout they will be logged out.

Florian-crg commented 3 years ago

I think this issue might be linked https://github.com/vercel/commerce/issues/531

okbel commented 2 years ago

Looks like this has been solved already!