diff --git a/framework/elasticpath/auth/use-login.tsx b/framework/elasticpath/auth/use-login.tsx index aa21f3dfe..3aa90ab72 100644 --- a/framework/elasticpath/auth/use-login.tsx +++ b/framework/elasticpath/auth/use-login.tsx @@ -2,15 +2,15 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useLogin, { UseLogin } from '@commerce/auth/use-login' -import type { LoginHook } from '../types/login' import useCustomer from '../customer/use-customer' -import epClient from '../utils/ep-client' +import Cookies from 'js-cookie' export default useLogin as UseLogin export const handler: MutationHook = { fetchOptions: { - query: '' + url: 'Customers', + method: 'TokenViaPassword', }, async fetcher({ input: { email, password }, options, fetch }) { if (!(email && password)) { @@ -19,8 +19,17 @@ export const handler: MutationHook = { 'A first name, last name, email and password are required to login', }) } + const {data:token} = await fetch({ + ...options, + variables:{ + params: [email, password] + } + }); - let token = await epClient.Customers.TokenViaPassword(email, password); + let expireTime = Math.round(token.expires/(1000*24*60*60)); + Cookies.set("user_token", + JSON.stringify(token), + { expires: expireTime }); return token || null; }, useHook: ({ fetch }) => () => { diff --git a/framework/elasticpath/auth/use-logout.tsx b/framework/elasticpath/auth/use-logout.tsx index 9b3fc3e44..9bea00e1a 100644 --- a/framework/elasticpath/auth/use-logout.tsx +++ b/framework/elasticpath/auth/use-logout.tsx @@ -1,17 +1,28 @@ import { MutationHook } from '@commerce/utils/types' import useLogout, { UseLogout } from '@commerce/auth/use-logout' +import Cookies from 'js-cookie' export default useLogout as UseLogout +import useCustomer from '../customer/use-customer' +import { useCallback } from 'react' export const handler: MutationHook = { fetchOptions: { - query: '', + query: '?', }, async fetcher() { - return null + Cookies.remove("user_token"); + }, + useHook: ({ fetch }) => () => { + const { mutate } = useCustomer() + + return useCallback( + async function logout() { + const data = await fetch() + await mutate(null, false) + return data + }, + [fetch, mutate] + ) }, - useHook: - ({ fetch }) => - () => - async () => {}, } diff --git a/framework/elasticpath/cart/use-cart.tsx b/framework/elasticpath/cart/use-cart.tsx index 4496c8fd2..8d5e24d7e 100644 --- a/framework/elasticpath/cart/use-cart.tsx +++ b/framework/elasticpath/cart/use-cart.tsx @@ -19,7 +19,6 @@ export const handler: SWRHook = { const response = useData({ swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, }) - console.log("checking data..", epClient.Cart()); return useMemo( () => Object.create(response, { diff --git a/framework/elasticpath/customer/use-customer.tsx b/framework/elasticpath/customer/use-customer.tsx index e5aaa68bb..4b71a79fb 100644 --- a/framework/elasticpath/customer/use-customer.tsx +++ b/framework/elasticpath/customer/use-customer.tsx @@ -1,27 +1,33 @@ import { SWRHook } from '@commerce/utils/types' import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' import getCustomerCookie from '../utils/get-customer-creds' -import epClient from '../utils/ep-client' +const creds = getCustomerCookie(); export default useCustomer as UseCustomer export const handler: SWRHook = { fetchOptions: { - query: '', + url: 'Customers', + method: 'Get', }, - async fetcher() { + async fetcher({fetch, options, input}) { const creds = getCustomerCookie(); - - // if user is not logged-in return null - if(!creds) { + if(!creds.id || !creds.token) { return null; } - console.log('moltin sdk', epClient); - const {data:customer} = await epClient.Customers.Get(creds.customer_id, creds.token); + + const {data} = await fetch({ + ...options, + variables:{ + params: [creds.id, creds.token] + } + }); + + console.log(data); return { - ...customer, - firstName: customer.name.split(" ")[0], - lastName: customer.name.split(" ")[1] + ...data, + firstName: data.name.split(" ")[0], + lastName: data.name.split(" ")[1] } }, useHook: ({ useData }) => (input) => { diff --git a/framework/elasticpath/types/customer.ts b/framework/elasticpath/types/customer.ts new file mode 100644 index 000000000..427bc0b03 --- /dev/null +++ b/framework/elasticpath/types/customer.ts @@ -0,0 +1,5 @@ +import * as Core from '@commerce/types/customer' + +export * from '@commerce/types/customer' + +export type CustomerSchema = Core.CustomerSchema diff --git a/framework/elasticpath/utils/get-customer-creds.js b/framework/elasticpath/utils/get-customer-creds.js index 8e1ae64ab..cdf4684ff 100644 --- a/framework/elasticpath/utils/get-customer-creds.js +++ b/framework/elasticpath/utils/get-customer-creds.js @@ -1,11 +1,10 @@ - import Cookies from 'js-cookie' const getCustomerCookie = () => { - const customerCookieObj = Cookies.get('customer_token'); + const customerCookieObj = Cookies.get("user_token"); if(customerCookieObj) { try { - return JSON.parse(atob(customerCookieObj)); + return JSON.parse(customerCookieObj); } catch(err) { return false; }