@@ -13,26 +13,24 @@ export const useSeamClient = ({
13
13
clientSessionToken ?: string
14
14
} ) : Seam => {
15
15
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
+ }
17
23
const clientSessionTokenFromCookie = getCookie ( `seam-clientSessionToken-${ userIdentifierKey } ` )
18
24
// if clientSessionToken is provided, use it, no need to check cookie or public key (create new client access token)
19
25
if ( clientSessionToken ?. startsWith ( 'seam_cst' ) ) {
20
26
// backend generated client access token
21
27
setCookie ( `seam-clientSessionToken-${ userIdentifierKey } ` , clientSessionToken , 30 )
22
- const seam = new Seam ( {
23
- clientSessionToken : clientSessionToken ,
24
- endpoint,
25
- } )
26
- return seam
28
+ return getSeam ( clientSessionToken )
27
29
} else if ( clientSessionTokenFromCookie ) {
28
30
// already have client access token from cookie
29
31
// no need to use public key
30
- const seam = new Seam ( {
31
- clientSessionToken : clientSessionTokenFromCookie ,
32
- endpoint,
33
- } )
34
32
// 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 )
36
34
} else if ( publishedKey ?. startsWith ( 'seam_pk1' ) ) {
37
35
// public key, need to create a client access token and store it in cookie
38
36
; ( async ( ) => {
@@ -45,11 +43,7 @@ export const useSeamClient = ({
45
43
throw new Error ( 'Could not get client access token' )
46
44
}
47
45
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 )
53
47
// TODO: improve: use devices are not loading probably due to async nature of this hook
54
48
} ) ( )
55
49
} else {
0 commit comments