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"
92import CommerceLayer , {
103 type CommerceLayerClient ,
114 type Customer ,
125} from "@commercelayer/sdk"
136import Cookies from "js-cookie"
147import { memoize } from "lodash-es"
158import { fireEvent } from "@/apis/event"
16- import { getKeyForCustomerToken , getKeyForGuestToken } from "@/apis/storage"
179import { pDebounce } from "@/utils/debounce"
1810import { type Config , getConfig } from "./config"
1911
2012export 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-
7725const 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
16475export 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
214130export 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)
0 commit comments