mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
Adding Moltin SDK for client side
Adding get user using sdk
This commit is contained in:
parent
82f14ef467
commit
c5d92d46ad
@ -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);
|
||||
|
@ -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<typeof handler>
|
||||
export const handler: SWRHook<any> = {
|
||||
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,
|
||||
},
|
||||
})
|
||||
},
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
16
framework/elasticpath/utils/get-customer-creds.js
Normal file
16
framework/elasticpath/utils/get-customer-creds.js
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user