Skip to content

Commit 36c37d8

Browse files
committed
Update use-seam-client.ts
1 parent febbd86 commit 36c37d8

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/hooks/use-seam-client.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@ export const useSeamClient = ({
1313
clientSessionToken?: string
1414
}): Seam => {
1515
if (typeof window === 'undefined') return null as any
16-
16+
if (!endpoint) throw new Error('No endpoint provided')
17+
const getSeam = (token: string) => {
18+
return new Seam({
19+
clientSessionToken: token,
20+
endpoint,
21+
})
22+
}
1723
const clientSessionTokenFromCookie = getCookie(`seam-clientSessionToken-${userIdentifierKey}`)
1824
// if clientSessionToken is provided, use it, no need to check cookie or public key (create new client access token)
1925
if (clientSessionToken?.startsWith('seam_cst')) {
2026
// backend generated client access token
2127
setCookie(`seam-clientSessionToken-${userIdentifierKey}`, clientSessionToken, 30)
22-
const seam = new Seam({
23-
clientSessionToken: clientSessionToken,
24-
endpoint,
25-
})
26-
return seam
28+
return getSeam(clientSessionToken)
2729
} else if (clientSessionTokenFromCookie) {
2830
// already have client access token from cookie
2931
// no need to use public key
30-
const seam = new Seam({
31-
clientSessionToken: clientSessionTokenFromCookie,
32-
endpoint,
33-
})
3432
// TODO: check if client access token is valid (not expired), if expired/invalid, get new one with the public key
35-
return seam
33+
return getSeam(clientSessionTokenFromCookie)
3634
} else if (publishedKey?.startsWith('seam_pk1')) {
3735
// public key, need to create a client access token and store it in cookie
3836
;(async () => {
@@ -45,11 +43,7 @@ export const useSeamClient = ({
4543
throw new Error('Could not get client access token')
4644
}
4745
setCookie(`seam-clientSessionToken-${userIdentifierKey}`, clientSessionToken, 30)
48-
const seam = new Seam({
49-
clientSessionToken: clientSessionToken,
50-
endpoint: endpoint,
51-
})
52-
return seam
46+
return getSeam(clientSessionToken)
5347
// TODO: improve: use devices are not loading probably due to async nature of this hook
5448
})()
5549
} else {

0 commit comments

Comments
 (0)