Skip to content

Commit fa0c0b0

Browse files
authored
Merge pull request #105 from apollographql/william/logout
NEBULA-1288: Add logout messages to parent
2 parents 2faaad6 + e2fbfc8 commit fa0c0b0

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

.changeset/tiny-camels-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@apollo/explorer': minor
3+
---
4+
5+
Add logout messages to parent

src/helpers/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export const EXPLORER_LISTENING_FOR_HANDSHAKE = 'ExplorerListeningForHandshake';
2727
export const HANDSHAKE_RESPONSE = 'HandshakeResponse';
2828
export const SET_PARTIAL_AUTHENTICATION_TOKEN_FOR_PARENT =
2929
'SetPartialAuthenticationTokenForParent';
30+
export const TRIGGER_LOGOUT_IN_PARENT = 'TriggerLogoutInParent';
3031
export const EXPLORER_LISTENING_FOR_PARTIAL_TOKEN =
3132
'ExplorerListeningForPartialToken';
3233
export const PARTIAL_AUTHENTICATION_TOKEN_RESPONSE =
3334
'PartialAuthenticationTokenResponse';
3435
export const INTROSPECTION_QUERY_WITH_HEADERS = 'IntrospectionQueryWithHeaders';
36+
export const PARENT_LOGOUT_SUCCESS = 'ParentLogoutSuccess';

src/helpers/postMessageRelayHelpers.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
SCHEMA_RESPONSE,
99
SET_PARTIAL_AUTHENTICATION_TOKEN_FOR_PARENT,
1010
EXPLORER_LISTENING_FOR_PARTIAL_TOKEN,
11+
PARENT_LOGOUT_SUCCESS,
12+
TRIGGER_LOGOUT_IN_PARENT,
1113
} from './constants';
1214
import type { JSONValue } from '../types';
1315

@@ -79,6 +81,9 @@ export type OutgoingEmbedMessage =
7981
status?: number;
8082
headers?: Headers;
8183
};
84+
}
85+
| {
86+
name: typeof PARENT_LOGOUT_SUCCESS;
8287
};
8388

8489
// TODO(Maya) uncomment and switch to MessageEvent as a generic when tsdx supports Typescript V4.
@@ -101,10 +106,14 @@ export type IncomingEmbedMessage = MessageEvent;
101106
// }>
102107
// | MessageEvent<{
103108
// name: typeof SET_PARTIAL_AUTHENTICATION_TOKEN_FOR_PARENT;
104-
// key: string;
109+
// localStorageKey: string;
105110
// partialToken: string;
106111
// }>
107112
// | MessageEvent<{
113+
// name: typeof TRIGGER_LOGOUT_IN_PARENT;
114+
// localStorageKey: string;
115+
// }>
116+
// | MessageEvent<{
108117
// name: typeof EXPLORER_LISTENING_FOR_PARTIAL_TOKEN;
109118
// localStorageKey?: string;
110119
// }>
@@ -276,6 +285,26 @@ export const handleAuthenticationPostMessage = ({
276285
);
277286
}
278287

288+
// When the embed logs out, remove the partial token in local storage
289+
if (data.name === TRIGGER_LOGOUT_IN_PARENT) {
290+
const partialEmbedApiKeysString = window.localStorage.getItem(
291+
'apolloStudioEmbeddedExplorerEncodedApiKey'
292+
);
293+
const partialEmbedApiKeys = partialEmbedApiKeysString
294+
? JSON.parse(partialEmbedApiKeysString)
295+
: {};
296+
delete partialEmbedApiKeys[data.localStorageKey];
297+
window.localStorage.setItem(
298+
'apolloStudioEmbeddedExplorerEncodedApiKey',
299+
JSON.stringify(partialEmbedApiKeys)
300+
);
301+
sendPostMessageToEmbed({
302+
message: { name: PARENT_LOGOUT_SUCCESS },
303+
embeddedIFrameElement,
304+
embedUrl,
305+
});
306+
}
307+
279308
if (
280309
data.name === EXPLORER_LISTENING_FOR_PARTIAL_TOKEN &&
281310
data.localStorageKey

0 commit comments

Comments
 (0)