Skip to content

Commit 52ee8a1

Browse files
deps: update js-auth to latest version
1 parent 0337f11 commit 52ee8a1

File tree

18 files changed

+1632
-1235
lines changed

18 files changed

+1632
-1235
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
"files.associations": {
1111
"*.css": "tailwindcss" // enforce usage of Tailwind extention to lint css files
1212
}
13-
}
13+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"@biomejs/biome": "2.2.4",
3737
"concurrently": "^9.2.1",
3838
"husky": "^9.1.7",
39-
"lerna": "^8.2.4",
40-
"lint-staged": "^16.2.0",
39+
"lerna": "^9.0.0",
40+
"lint-staged": "^16.2.3",
4141
"replace-in-file": "^8.3.0",
4242
"typescript": "^5.9.2"
4343
},

packages/docs/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"@babel/preset-env": "^7.28.3",
2222
"@babel/preset-react": "^7.27.1",
2323
"@babel/preset-typescript": "^7.27.1",
24-
"@storybook/addon-docs": "^9.1.7",
25-
"@storybook/addon-links": "^9.1.7",
26-
"@storybook/html-vite": "^9.1.7",
24+
"@storybook/addon-docs": "^9.1.9",
25+
"@storybook/addon-links": "^9.1.9",
26+
"@storybook/html-vite": "^9.1.9",
2727
"@storybook/icons": "^1.6.0",
2828
"@types/common-tags": "^1.8.4",
29-
"@types/node": "^20.19.17",
29+
"@types/node": "^20.19.18",
3030
"@types/react": "^19",
3131
"@types/react-syntax-highlighter": "^15.5.13",
3232
"babel-loader": "^10.0.0",
@@ -37,8 +37,8 @@
3737
"react-dom": "^19",
3838
"react-syntax-highlighter": "^15.6.6",
3939
"remark-gfm": "^4.0.1",
40-
"sass": "^1.93.0",
41-
"storybook": "^9.1.7",
40+
"sass": "^1.93.2",
41+
"storybook": "^9.1.9",
4242
"typescript": "^5.9.2",
4343
"vite": "^7.1.7"
4444
},

packages/drop-in/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
"email": "marco.montalbano@commercelayer.io"
4646
},
4747
"dependencies": {
48-
"@commercelayer/js-auth": "^6.7.2",
48+
"@commercelayer/js-auth": "^7.0.0",
4949
"@commercelayer/organization-config": "^2.4.0",
50-
"@commercelayer/sdk": "^6.46.0",
50+
"@commercelayer/sdk": "^6.47.0",
5151
"@types/lodash-es": "^4.17.12",
5252
"iframe-resizer": "4.3.11",
5353
"js-cookie": "^3.0.5",
@@ -63,12 +63,13 @@
6363
"@types/iframe-resizer": "^4.0.0",
6464
"@types/jest": "^29.5.14",
6565
"@types/js-cookie": "^3.0.6",
66-
"@types/node": "^20.19.17",
66+
"@types/node": "^20.19.18",
6767
"jest": "^29.7.0",
6868
"jest-cli": "^29.7.0",
69+
"lodash": "^4.17.21",
6970
"nodemon": "^3.1.10",
70-
"puppeteer": "24.22.0",
71-
"sass": "^1.93.0",
71+
"puppeteer": "24.22.3",
72+
"sass": "^1.93.2",
7273
"ts-jest": "^29.4.4",
7374
"typescript": "^5.9.2"
7475
},

packages/drop-in/src/apis/commercelayer/account.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { getClosestLocationHref } from "@/utils/url"
44

55
export async function getMyAccountUrl(): Promise<string | undefined> {
66
const config = getConfig()
7-
const { type } = await getAccessToken(config)
7+
const { ownerType } = await getAccessToken(config)
88

9-
if (type === "guest") {
9+
if (ownerType === "guest") {
1010
return undefined
1111
}
1212

packages/drop-in/src/apis/commercelayer/cart.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function createEmptyCart(): Promise<Order> {
2727
config.defaultAttributes?.orders ?? {},
2828
)
2929

30-
if (token.type === "guest") {
30+
if (token.ownerType === "guest") {
3131
setCartId(order.id)
3232
}
3333

@@ -142,7 +142,7 @@ export async function _getCart(): Promise<Order | null> {
142142
include: ["line_items.item", "line_items.line_item_options.sku_option"],
143143
}
144144

145-
if (token.type === "guest") {
145+
if (token.ownerType === "guest") {
146146
const orderId = getCartId()
147147

148148
if (orderId === null) {
@@ -167,7 +167,7 @@ export async function _getCart(): Promise<Order | null> {
167167
return null
168168
}
169169

170-
const [order = null] = await client.customers.orders(token.customerId, {
170+
const [order = null] = await client.customers.orders(token.ownerId, {
171171
...orderParams,
172172
filters: {
173173
market_id_in: jwt.payload.market.id.join(","),

packages/drop-in/src/apis/commercelayer/client.ts

Lines changed: 51 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,27 @@
1-
import {
2-
type AuthenticateOptions,
3-
type AuthenticateReturn,
4-
authenticate,
5-
jwtDecode,
6-
jwtIsSalesChannel,
7-
revoke,
8-
} from "@commercelayer/js-auth"
1+
import { makeSalesChannel } from "@commercelayer/js-auth"
92
import CommerceLayer, {
103
type CommerceLayerClient,
114
type Customer,
125
} from "@commercelayer/sdk"
136
import Cookies from "js-cookie"
147
import { memoize } from "lodash-es"
158
import { fireEvent } from "@/apis/event"
16-
import { getKeyForCustomerToken, getKeyForGuestToken } from "@/apis/storage"
179
import { pDebounce } from "@/utils/debounce"
1810
import { type Config, getConfig } from "./config"
1911

2012
export type Token = (
2113
| {
22-
type: "guest"
14+
ownerType: "guest"
2315
}
2416
| {
25-
type: "customer"
26-
customerId: string
17+
ownerType: "customer"
18+
ownerId: string
2719
}
2820
) & {
2921
accessToken: string
3022
scope?: string
3123
}
3224

33-
function setToken(key: string, value: Token, expires?: Date): void {
34-
Cookies.set(key, JSON.stringify(value), { expires })
35-
}
36-
37-
function clearToken(key: string): void {
38-
Cookies.remove(key)
39-
}
40-
41-
function getToken(key: string): Token | undefined {
42-
const cookie = Cookies.get(key)
43-
44-
if (cookie == null) {
45-
return undefined
46-
}
47-
48-
try {
49-
return JSON.parse(cookie)
50-
} catch (_e) {
51-
return undefined
52-
}
53-
}
54-
55-
async function revokeToken(
56-
clientCredentials: AuthenticateOptions<"client_credentials">,
57-
accessToken: string,
58-
): Promise<boolean> {
59-
const res = await revoke({
60-
clientId: clientCredentials.clientId,
61-
token: accessToken,
62-
})
63-
64-
return res.errors == null
65-
}
66-
67-
async function getSalesChannelToken(
68-
clientCredentials: AuthenticateOptions<"client_credentials">,
69-
): Promise<AuthenticateReturn<"client_credentials"> | null> {
70-
return await authenticate("client_credentials", {
71-
clientId: clientCredentials.clientId,
72-
scope: clientCredentials.scope,
73-
domain: clientCredentials.domain,
74-
}).then((res) => (res.errors == null ? res : null))
75-
}
76-
7725
const getCustomerInfoFromUrl = (): {
7826
accessToken: string | undefined
7927
scope: string | undefined
@@ -93,82 +41,50 @@ const getCustomerInfoFromUrl = (): {
9341
}
9442
}
9543

96-
async function readCustomerToken(
97-
clientCredentials: AuthenticateOptions<"client_credentials">,
98-
): Promise<Token | null> {
99-
const cookieName = getKeyForCustomerToken(clientCredentials)
100-
const cookieValue = getToken(cookieName) ?? null
101-
102-
// const searchParams = new window.URL(window.location.href).searchParams
103-
// const accessToken = searchParams.get('accessToken')
104-
// const scope = searchParams.get('scope') ?? clientCredentials.scope
105-
const { accessToken, scope = clientCredentials.scope } =
106-
getCustomerInfoFromUrl()
107-
108-
if (accessToken == null) {
109-
return cookieValue
110-
}
111-
112-
const jwt = jwtDecode(accessToken)
113-
114-
if (jwtIsSalesChannel(jwt.payload) && jwt.payload.owner != null) {
115-
const token: Token = {
116-
type: "customer",
117-
customerId: jwt.payload.owner.id,
118-
accessToken,
119-
scope,
120-
}
121-
122-
setToken(cookieName, token, new Date(jwt.payload.exp * 1000))
123-
124-
return token
125-
}
126-
127-
return cookieValue
128-
}
129-
130-
async function readGuestToken(
131-
clientCredentials: AuthenticateOptions<"client_credentials">,
132-
): Promise<Token> {
133-
const cookieName = getKeyForGuestToken(clientCredentials)
134-
const value = getToken(cookieName)
135-
136-
if (value !== undefined) {
137-
return value
138-
}
139-
140-
const salesChannelToken = await getSalesChannelToken(clientCredentials).catch(
141-
(error) => {
142-
throw new Error(
143-
`Cannot get a sales channel token. ${error.body.error}. ${error.body.error_description}`,
144-
)
145-
},
146-
)
147-
148-
if (salesChannelToken == null) {
149-
throw new Error("Unable to get a valid sales channel token.")
150-
}
151-
152-
const { accessToken, expires } = salesChannelToken
153-
154-
const token: Token = {
155-
type: "guest",
156-
accessToken,
157-
scope: clientCredentials.scope,
158-
}
159-
setToken(cookieName, token, expires)
44+
const getSalesChannel = memoize(
45+
(clientCredentials: Parameters<typeof makeSalesChannel>[0]) =>
46+
makeSalesChannel(clientCredentials, {
47+
async getKey(configuration, type) {
48+
const scope = (configuration.scope ?? "undefined").replace(" ", "-")
49+
const t = type === "guest" ? "token" : "session"
50+
51+
return `commercelayer_${t}-${configuration.clientId}-${scope}`
52+
},
53+
storage: {
54+
async getItem(key: string) {
55+
return JSON.parse(Cookies.get(key) ?? "null")
56+
},
57+
async removeItem(key) {
58+
Cookies.remove(key)
59+
},
60+
async setItem(key, value) {
61+
Cookies.set(key, JSON.stringify(value))
62+
},
63+
},
64+
}),
65+
(clientCredentials) => JSON.stringify(clientCredentials),
66+
)
16067

161-
return token
162-
}
68+
const configToClientCredentials = (config: Config) => ({
69+
clientId: config.clientId,
70+
scope: config.scope,
71+
debug: config.debug !== "none",
72+
domain: config.domain,
73+
})
16374

16475
export const getAccessToken = memoize(
165-
async (
166-
clientCredentials: AuthenticateOptions<"client_credentials">,
167-
): Promise<Token> => {
168-
let token = await readCustomerToken(clientCredentials)
169-
170-
if (token == null) {
171-
token = await readGuestToken(clientCredentials)
76+
async (config: Config): Promise<Token> => {
77+
const clientCredentials = configToClientCredentials(config)
78+
const salesChannel = getSalesChannel(clientCredentials)
79+
let token = await salesChannel.getAuthorization()
80+
81+
const { accessToken, scope = config.scope } = getCustomerInfoFromUrl()
82+
83+
if (accessToken != null && scope != null) {
84+
token = await salesChannel.setCustomer({
85+
accessToken,
86+
scope,
87+
})
17288
}
17389

17490
fireEvent("cl-identity-gettoken", [], token)
@@ -197,9 +113,9 @@ async function _getCustomer(): Promise<Customer | null> {
197113
const config = getConfig()
198114
const token = await getAccessToken(config)
199115

200-
if (token.type === "customer") {
116+
if (token.ownerType === "customer") {
201117
const client = await createClient(config)
202-
return await client.customers.retrieve(token.customerId, {
118+
return await client.customers.retrieve(token.ownerId, {
203119
fields: customerFields,
204120
})
205121
}
@@ -213,13 +129,12 @@ export const getCustomer = memoize(
213129

214130
export async function logout(): Promise<void> {
215131
const config = getConfig()
132+
const clientCredentials = configToClientCredentials(config)
133+
const salesChannel = getSalesChannel(clientCredentials)
216134
const token = await getAccessToken(config)
217135

218-
if (token.type === "customer") {
219-
const cookieName = getKeyForCustomerToken(config)
220-
clearToken(cookieName)
221-
222-
await revokeToken(config, token.accessToken)
136+
if (token.ownerType === "customer") {
137+
await salesChannel.logoutCustomer()
223138

224139
getAccessToken.cache.clear?.()
225140
// await getAccessToken(config)
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { AuthenticateOptions } from "@commercelayer/js-auth"
21
import type { Config } from "./commercelayer/config"
32

43
const prefix = "commercelayer_"
@@ -12,19 +11,3 @@ export function getKeyForCart(config: Config): string {
1211

1312
return `${prefix}order-id-${config.clientId}-${scope}${suffix}`
1413
}
15-
16-
export function getKeyForGuestToken(
17-
clientCredentials: AuthenticateOptions<"client_credentials">,
18-
): string {
19-
const scope = (clientCredentials.scope ?? "undefined").replace(" ", "-")
20-
21-
return `${prefix}token-${clientCredentials.clientId}-${scope}`
22-
}
23-
24-
export function getKeyForCustomerToken(
25-
clientCredentials: AuthenticateOptions<"client_credentials">,
26-
): string {
27-
const scope = (clientCredentials.scope ?? "undefined").replace(" ", "-")
28-
29-
return `${prefix}session-${clientCredentials.clientId}-${scope}`
30-
}

packages/drop-in/src/components/cl-cart-count/cl-cart-count.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ClCartCount } from "./cl-cart-count"
77
describe("cl-cart-count.spec", () => {
88
it("renders", async () => {
99
jest.spyOn(client, "getAccessToken").mockResolvedValue({
10-
type: "guest",
10+
ownerType: "guest",
1111
accessToken: mockedAccessToken,
1212
scope: "market:code:usa",
1313
})
@@ -26,7 +26,7 @@ describe("cl-cart-count.spec", () => {
2626

2727
it('renders without content when "hide-when-empty" attribute is set to `true`', async () => {
2828
jest.spyOn(client, "getAccessToken").mockResolvedValue({
29-
type: "guest",
29+
ownerType: "guest",
3030
accessToken: mockedAccessToken,
3131
scope: "market:code:usa",
3232
})

0 commit comments

Comments
 (0)