Adding Moltin SDK for client side

Adding get user using sdk
This commit is contained in:
GunaTrika 2021-07-28 15:53:19 +05:30
parent 82f14ef467
commit c5d92d46ad
4 changed files with 47 additions and 6 deletions

View File

@ -3,7 +3,7 @@ import type { LoginEndpoint } from '.'
const MoltinGateway = require('@moltin/sdk').gateway const MoltinGateway = require('@moltin/sdk').gateway
const Moltin = MoltinGateway({ const Moltin = MoltinGateway({
client_id: process.env.ELASTICPATH_CLIENTID, client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID,
client_secret: process.env.ELASTICPATH_SECRET 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); let expiry = new Date(Date.now() + tokens.data.expires);
// encodeing the tocken object with btoa // encodeing the tocken object with btoa
// in clinet side, use atob to decode the token object // 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); res.setHeader("Set-Cookie", cookieValue);
return res.status(200).json(tokens); return res.status(200).json(tokens);

View File

@ -1,15 +1,33 @@
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' 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<typeof handler> export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<any> = { export const handler: SWRHook<any> = {
fetchOptions: { fetchOptions: {
query: '', query: '',
}, },
async fetcher({ input, options, fetch }) {}, async fetcher() {
useHook: () => () => { const creds = getCustomerCookie();
return async function addItem() {
return {} // 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,
},
})
}, },
} }

View File

@ -5,4 +5,11 @@ module.exports = {
images: { images: {
domains: ['localhost'], domains: ['localhost'],
}, },
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback.fs = false;
config.resolve.fallback.constants = false;
}
return config;
}
} }

View File

@ -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;