File tree Expand file tree Collapse file tree
src/puter-js/src/modules/networking Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,19 +25,38 @@ function getWispRequestHeaders () {
2525 return headers ;
2626}
2727
28+ async function ensureWispAuthentication ( ) {
29+ const puter = getPuterInstance ( ) ;
30+ if ( puter . authToken ) {
31+ return ;
32+ }
33+
34+ await puter . ui . authenticateWithPuter ( ) ;
35+ }
36+
2837function getClientCacheKey ( ) {
2938 const puter = getPuterInstance ( ) ;
3039 return `${ puter . APIOrigin } ::${ puter . authToken || '' } ` ;
3140}
3241
33- export async function getWispCredentials ( ) {
42+ export async function getWispCredentials ( retryAuth = true ) {
3443 const puter = getPuterInstance ( ) ;
44+ await ensureWispAuthentication ( ) ;
45+
3546 const response = await fetch ( `${ puter . APIOrigin } /wisp/relay-token/create` , {
3647 method : 'POST' ,
3748 headers : getWispRequestHeaders ( ) ,
3849 body : JSON . stringify ( { } ) ,
3950 } ) ;
4051
52+ if ( response . status === 401 && retryAuth ) {
53+ if ( typeof puter . resetAuthToken === 'function' ) {
54+ puter . resetAuthToken ( ) ;
55+ }
56+ await ensureWispAuthentication ( ) ;
57+ return await getWispCredentials ( false ) ;
58+ }
59+
4160 if ( ! response . ok ) {
4261 throw new Error (
4362 `Failed to create relay token (HTTP ${ response . status } ${ response . statusText } ).` ,
You can’t perform that action at this time.
0 commit comments