Skip to content

Commit ab34a2f

Browse files
committed
force wisp auth
1 parent 944da74 commit ab34a2f

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

  • src/puter-js/src/modules/networking

src/puter-js/src/modules/networking/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
2837
function 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}).`,

0 commit comments

Comments
 (0)