diff --git a/framework/elasticpath/api/endpoints/login/login.ts b/framework/elasticpath/api/endpoints/login/login.ts index 829a97e3a..b3336c24a 100644 --- a/framework/elasticpath/api/endpoints/login/login.ts +++ b/framework/elasticpath/api/endpoints/login/login.ts @@ -3,7 +3,7 @@ import type { LoginEndpoint } from '.' const MoltinGateway = require('@moltin/sdk').gateway const Moltin = MoltinGateway({ - client_id: process.env.ELASTICPATH_CLIENTID, + client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID, client_secret: process.env.ELASTICPATH_SECRET }) @@ -33,7 +33,7 @@ const login: LoginEndpoint['handlers']['login'] = async ({ let expiry = new Date(Date.now() + tokens.data.expires); // encodeing the tocken object with btoa // in clinet side, use atob to decode the token object - let cookieValue = `customer_token=${btoa(customer_token)};Expires=${expiry}` + let cookieValue = `customer_token=${btoa(customer_token)};Expires=${expiry};Path=/` res.setHeader("Set-Cookie", cookieValue); return res.status(200).json(tokens); diff --git a/framework/elasticpath/customer/use-customer.tsx b/framework/elasticpath/customer/use-customer.tsx index 41757cd0d..e6bb1b3bc 100644 --- a/framework/elasticpath/customer/use-customer.tsx +++ b/framework/elasticpath/customer/use-customer.tsx @@ -1,15 +1,33 @@ import { SWRHook } from '@commerce/utils/types' import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' +import { gateway as MoltinGateway } from '@moltin/sdk' +import getCustomerCookie from '../utils/get-customer-creds' + +const Moltin = MoltinGateway({ + client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID +}) export default useCustomer as UseCustomer export const handler: SWRHook = { fetchOptions: { query: '', }, - async fetcher({ input, options, fetch }) {}, - useHook: () => () => { - return async function addItem() { - return {} + async fetcher() { + const creds = getCustomerCookie(); + + // if user is not logged-in return null + if(!creds) { + return null; } + const data = await Moltin.Customers.Get(creds.customer_id, creds.token); + return data || null; + }, + useHook: ({ useData }) => (input) => { + return useData({ + swrOptions: { + revalidateOnFocus: false, + ...input?.swrOptions, + }, + }) }, } diff --git a/framework/elasticpath/next.config.js b/framework/elasticpath/next.config.js index ce46b706f..eb0d034cd 100644 --- a/framework/elasticpath/next.config.js +++ b/framework/elasticpath/next.config.js @@ -5,4 +5,11 @@ module.exports = { images: { domains: ['localhost'], }, + webpack: (config, { isServer }) => { + if (!isServer) { + config.resolve.fallback.fs = false; + config.resolve.fallback.constants = false; + } + return config; + } } diff --git a/framework/elasticpath/utils/get-customer-creds.js b/framework/elasticpath/utils/get-customer-creds.js new file mode 100644 index 000000000..8e1ae64ab --- /dev/null +++ b/framework/elasticpath/utils/get-customer-creds.js @@ -0,0 +1,16 @@ + +import Cookies from 'js-cookie' + +const getCustomerCookie = () => { + const customerCookieObj = Cookies.get('customer_token'); + if(customerCookieObj) { + try { + return JSON.parse(atob(customerCookieObj)); + } catch(err) { + return false; + } + } else { + return false; + } +} +export default getCustomerCookie; \ No newline at end of file