Merge pull request #437 from nhost/fix/broadcast-channel-react-native

fix: avoid error when BroadcastChannell is not available
This commit is contained in:
Pilou
2022-04-21 16:10:49 +02:00
committed by GitHub
3 changed files with 23 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
'@nhost/core': patch
---
Avoid error when BroadcastChannell is not available

View File

@@ -44,13 +44,17 @@ export class AuthClient {
}
if (typeof window !== 'undefined' && autoSignIn) {
this._channel = new BroadcastChannel('nhost')
this._channel.addEventListener('message', (token) => {
const existingToken = this.interpreter?.state.context.refreshToken
if (this.interpreter && token.data !== existingToken) {
this.interpreter.send({ type: 'TRY_TOKEN', token: token.data })
}
})
try {
this._channel = new BroadcastChannel('nhost')
this._channel.addEventListener('message', (token) => {
const existingToken = this.interpreter?.state.context.refreshToken
if (this.interpreter && token.data !== existingToken) {
this.interpreter.send({ type: 'TRY_TOKEN', token: token.data })
}
})
} catch (error) {
// * BroadcastChannel is not available e.g. react-native
}
}
}

View File

@@ -659,9 +659,13 @@ export const createAuthMachine = ({
// TODO remove the hash. For the moment, it is kept to avoid regression from the current SDK.
// * Then, only `refreshToken` will be in the hash, while `type` will be sent by hasura-auth as a query parameter
// window.history.pushState({}, '', location.pathname)
const channel = new BroadcastChannel('nhost')
// ? broadcat session instead of token ?
channel.postMessage(refreshToken)
try {
const channel = new BroadcastChannel('nhost')
// ? broadcat session instead of token ?
channel.postMessage(refreshToken)
} catch (error) {
// * BroadcastChannel is not available e.g. react-native
}
return { session }
},
importRefreshToken: async () => {