@@ -2,6 +2,7 @@ import type Query from '../../../base/Query'
22import type RemoteCommand from '../../../base/RemoteCommand'
33import { forEachQuery } from '../../../base/QueryCache'
44
5+ // TODO: Why is this in here? It seems general-purpose not auth-specific
56function dirtyAllQueries () {
67 forEachQuery((query: Query< RemoteCommand < any , any, any > > ) => {
78 query.setDirty()
@@ -10,29 +11,49 @@ function dirtyAllQueries () {
1011
1112const accessTokens = new Map< string , string > ()
1213
14+ interface AuthMessage {
15+ type: 'login' | 'logout'
16+ baseUrl: string
17+ }
18+
19+ const login = (baseUrl: string, accessToken: string): void => {
20+ accessTokens.set(baseUrl, accessToken)
21+ dirtyAllQueries()
22+ }
23+
1324const logout = (urlBase: string): void => {
1425 accessTokens.delete(urlBase)
1526 dirtyAllQueries()
1627}
28+
29+ // These functions gets overridden with a form that broadcasts the event if BroadcastChannel is available
30+ let handleLogin: (baseUrl: string, accessToken: string) => void = login
1731let handleLogout: (baseUrl: string) => void = logout
1832
1933const tokenForUrl = (baseUrl: string): string | undefined => accessTokens.get(baseUrl)
20- const handleLogin: (baseUrl: string, accessToken: string) => void = (baseUrl, accessToken) => {
21- accessTokens.set(baseUrl, accessToken)
22- dirtyAllQueries()
23- }
2434
2535if (typeof BroadcastChannel !== 'undefined') {
2636 const logoutChannel = new BroadcastChannel('foobara-auth-events')
2737
28- logoutChannel.addEventListener('message', (event: MessageEvent< string > ) => {
29- accessTokens.delete(event.data)
30- deleteAllQueries()
38+ logoutChannel.addEventListener('message', (event: MessageEvent< AuthMessage > ) => {
39+ const { type, baseUrl } = event.data
40+
41+ if (type === 'login') {
42+ dirtyAllQueries()
43+ } else if (type === 'logout') {
44+ logout(baseUrl)
45+ } else {
46+ throw new Error(`Unknown auth message type: ${JSON.stringify(type)}`)
47+ }
3148 })
3249
3350 handleLogout = (baseUrl: string) => {
3451 logout(baseUrl)
35- logoutChannel.postMessage(baseUrl)
52+ logoutChannel.postMessage({ baseUrl, type: 'logout' })
53+ }
54+ handleLogin = (baseUrl: string, accessToken: string) => {
55+ login(baseUrl, accessToken)
56+ logoutChannel.postMessage({ baseUrl, type: 'login' })
3657 }
3758}
3859
0 commit comments