mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-23 14:17:06 +00:00
fix(puter-js): a godmode app opened in its own tab stays signed in across a reload (#4632)
A tab the desktop opened for an app boots the SDK as a top-level page, where a session-type token was kept in memory only. Once the desktop hands such a tab its launch token, the SDK now keeps it in sessionStorage, as a framed app does, and a reload of that tab reads it back before any stored app token. A page that wasn't handed a token this way still persists no session token.
This commit is contained in:
1 parent
ac44de819d
commit
d1cbfb9fb1
2 files changed
+94
-4
No files matched your search
@@ -277,6 +277,15 @@ export class Puter {
|
||||
*/
|
||||
openerLaunchItem_ = null;
|
||||
|
||||
/**
|
||||
* Whether this is a tab the GUI opened for an app and handed a session
|
||||
* token, which it then keeps in sessionStorage the way a framed app does.
|
||||
*
|
||||
* @internal
|
||||
* @type {boolean}
|
||||
*/
|
||||
appTabSession_ = false;
|
||||
|
||||
// Holds the unique app instance ID that is provided by the host environment
|
||||
appInstanceID;
|
||||
|
||||
@@ -751,6 +760,9 @@ export class Puter {
|
||||
// the SDK needs to show a dialog to the user to ask for permission to access their Puter account.
|
||||
else if (this.env === 'web') {
|
||||
try {
|
||||
// A godmode app reloading in the tab the GUI opened it in;
|
||||
// only that handoff stores a session token on this origin.
|
||||
const godmodeToken = this.readStoredSessionToken_();
|
||||
const storedToken = this.normalizeAuthTokenCandidate(
|
||||
localStorage.getItem(STORAGE_KEY_V2),
|
||||
);
|
||||
@@ -763,7 +775,10 @@ export class Puter {
|
||||
const boundOrigin = this.normalizeStringCandidate(
|
||||
localStorage.getItem(STORAGE_KEY_ORIGIN_V2),
|
||||
);
|
||||
if (
|
||||
if (godmodeToken) {
|
||||
this.appTabSession_ = true;
|
||||
this.setAuthToken(godmodeToken);
|
||||
} else if (
|
||||
storedToken &&
|
||||
this._storedTokenUsableForCurrentOrigin(boundOrigin)
|
||||
) {
|
||||
@@ -800,7 +815,12 @@ export class Puter {
|
||||
launch.item,
|
||||
);
|
||||
}
|
||||
if (launch?.token) this.setAuthToken(launch.token);
|
||||
if (launch?.token) {
|
||||
this.appTabSession_ = this.isUserSessionToken_(
|
||||
launch.token,
|
||||
);
|
||||
this.setAuthToken(launch.token);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1041,10 +1061,10 @@ export class Puter {
|
||||
|
||||
// If the SDK is running on a 3rd-party site or an app, then save the authToken in localStorage.
|
||||
// A user session token (a godmode app's launch token) never goes there:
|
||||
// an app keeps it in sessionStorage so a frame reload still has it.
|
||||
// an app keeps it in sessionStorage so a reload still has it.
|
||||
if (this.env === 'web' || this.env === 'app') {
|
||||
this.storeSessionToken_(
|
||||
this.env === 'app' &&
|
||||
(this.env === 'app' || this.appTabSession_) &&
|
||||
this.isUserSessionToken_(normalizedAuthToken)
|
||||
? normalizedAuthToken
|
||||
: null,
|
||||
|
||||
@@ -286,6 +286,76 @@ describe('web-mode stored token', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('godmode app in a tab the GUI opened', () => {
|
||||
const TAB = '?puter.app_instance_id=i';
|
||||
|
||||
/** Boots a top-level tab whose GUI opener hands it `token`. */
|
||||
const bootTab = async (token) => {
|
||||
const opener = {
|
||||
closed: false,
|
||||
postMessage: (data, guiOrigin) =>
|
||||
window.dispatchEvent(
|
||||
new MessageEvent('message', {
|
||||
origin: guiOrigin,
|
||||
source: opener,
|
||||
data: {
|
||||
msg: 'puter.launch',
|
||||
appInstanceID: data.appInstanceID,
|
||||
token,
|
||||
},
|
||||
}),
|
||||
),
|
||||
};
|
||||
Object.defineProperty(globalThis, 'opener', {
|
||||
value: opener,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
const puter = await bootWeb(TAB);
|
||||
await puter.openerLaunch_;
|
||||
return puter;
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.opener = null;
|
||||
});
|
||||
|
||||
it('keeps the handed-over token in sessionStorage, so a reload has it', async () => {
|
||||
const puter = await bootTab(GODMODE_TOKEN);
|
||||
expect(puter.env).toBe('web');
|
||||
expect(puter.authToken).toBe(GODMODE_TOKEN);
|
||||
expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
|
||||
expect(sessionStorage.getItem(STORAGE_KEY)).toBe(GODMODE_TOKEN);
|
||||
|
||||
// The opener was cut off, so the reload has only what it stored.
|
||||
expect(globalThis.opener).toBeNull();
|
||||
const reloaded = await bootWeb(TAB);
|
||||
expect(reloaded.authToken).toBe(GODMODE_TOKEN);
|
||||
expect(sessionStorage.getItem(STORAGE_KEY)).toBe(GODMODE_TOKEN);
|
||||
});
|
||||
|
||||
it('reloads on the godmode token over an app token stored earlier', async () => {
|
||||
localStorage.setItem(STORAGE_KEY, APP_TOKEN);
|
||||
localStorage.setItem(ORIGIN_KEY, 'https://api.puter.com');
|
||||
await bootTab(GODMODE_TOKEN);
|
||||
|
||||
const reloaded = await bootWeb(TAB);
|
||||
expect(reloaded.authToken).toBe(GODMODE_TOKEN);
|
||||
});
|
||||
|
||||
it('drops the sessionStorage copy on sign-out', async () => {
|
||||
const puter = await bootTab(GODMODE_TOKEN);
|
||||
puter.resetAuthToken();
|
||||
expect(sessionStorage.getItem(STORAGE_KEY)).toBeNull();
|
||||
});
|
||||
|
||||
it('still keeps an app token in localStorage only', async () => {
|
||||
await bootTab(APP_TOKEN);
|
||||
expect(localStorage.getItem(STORAGE_KEY)).toBe(APP_TOKEN);
|
||||
expect(sessionStorage.getItem(STORAGE_KEY)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GUI cache refresh', () => {
|
||||
it('keeps one refresh loop however often the token is set', async () => {
|
||||
const puter = await bootApp(`${LAUNCH}${APP_TOKEN}`);
|
||||
|
||||
Reference in new issue
Block a user