mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
41 lines
853 B
TypeScript
41 lines
853 B
TypeScript
import Cookies from 'js-cookie'
|
|
import { getSalesChannelToken } from '@commercelayer/js-auth'
|
|
|
|
type GetTokenObj = {
|
|
clientId?: string
|
|
endpoint?: string
|
|
scope?: string
|
|
user?: any
|
|
}
|
|
|
|
export async function getToken({
|
|
clientId,
|
|
endpoint,
|
|
scope = 'market:all',
|
|
user,
|
|
}: GetTokenObj) {
|
|
const getCookieToken = Cookies.get('clAccessToken')
|
|
if (!getCookieToken && clientId && endpoint) {
|
|
const auth = await getSalesChannelToken(
|
|
{
|
|
clientId,
|
|
endpoint,
|
|
scope,
|
|
},
|
|
user
|
|
)
|
|
Cookies.set('clAccessToken', auth?.accessToken as string, {
|
|
// @ts-ignore
|
|
expires: auth?.expires,
|
|
})
|
|
return auth
|
|
? {
|
|
accessToken: auth.accessToken,
|
|
customerId: auth.data.owner_id,
|
|
...auth.data,
|
|
}
|
|
: null
|
|
}
|
|
return { accessToken: getCookieToken }
|
|
}
|