@@ -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,53 @@ function dirtyAllQueries () {
1011
1112const accessTokens = new Map< string , string > ()
1213
14+ interface AuthMessage {
15+ type: 'login' | 'logout'
16+ baseUrl: string
17+ accessToken?: string
18+ }
19+
20+ const login = (baseUrl: string, accessToken: string): void => {
21+ accessTokens.set(baseUrl, accessToken)
22+ dirtyAllQueries()
23+ }
24+
1325const logout = (urlBase: string): void => {
1426 accessTokens.delete(urlBase)
1527 dirtyAllQueries()
1628}
29+
30+ // These functions gets overridden with a form that broadcasts the event if BroadcastChannel is available
31+ let handleLogin: (baseUrl: string, accessToken: string) => void = login
1732let handleLogout: (baseUrl: string) => void = logout
1833
1934const 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- }
2435
2536if (typeof BroadcastChannel !== 'undefined') {
2637 const logoutChannel = new BroadcastChannel('foobara-auth-events')
2738
28- logoutChannel.addEventListener('message', (event: MessageEvent< string > ) => {
29- accessTokens.delete(event.data)
30- deleteAllQueries()
39+ logoutChannel.addEventListener('message', (event: MessageEvent< AuthMessage > ) => {
40+ const { type, baseUrl, accessToken } = event.data
41+
42+ if (type === 'login') {
43+ if (accessToken != null) {
44+ login(baseUrl, accessToken)
45+ }
46+ } else if (type === 'logout') {
47+ logout(baseUrl)
48+ } else {
49+ throw new Error(`Unknown auth message type: ${JSON.stringify(type)}`)
50+ }
3151 })
3252
3353 handleLogout = (baseUrl: string) => {
3454 logout(baseUrl)
35- logoutChannel.postMessage(baseUrl)
55+ logoutChannel.postMessage({ baseUrl, type: 'logout' })
56+ }
57+ handleLogin = (baseUrl: string, accessToken: string) => {
58+ login(baseUrl, accessToken)
59+ // TODO: is it safe to send an accessToken over BroadcastChannel?
60+ logoutChannel.postMessage({ baseUrl, type: 'login', accessToken })
3661 }
3762}
3863
0 commit comments