Update race condition with Apollo getting ID Token and BackendUserInfo

This commit is contained in:
Andrew Bastin
2021-05-13 22:42:04 -04:00
parent b9c3219094
commit e7dd67deaa

View File

@@ -4,30 +4,15 @@ import { setContext } from "@apollo/client/link/context"
import { fb } from "./fb" import { fb } from "./fb"
import { getMainDefinition } from "@apollo/client/utilities" import { getMainDefinition } from "@apollo/client/utilities"
/**
* Stores the current authentication token
*
* The token stored here is the Firebase Auth token.
* If null, no token is passed (no user signed in)
*/
let authToken: string | null = null
/*
* Updates the token value on Firebase ID Token changes
*/
fb.idToken$.subscribe((newToken) => {
authToken = newToken
})
/** /**
* Injects auth token if available * Injects auth token if available
*/ */
const authLink = setContext((_, { headers }) => { const authLink = setContext((_, { headers }) => {
if (authToken) { if (fb.idToken) {
return { return {
headers: { headers: {
...headers, ...headers,
authorization: `Bearer ${authToken}`, authorization: `Bearer ${fb.idToken}`,
}, },
} }
} else { } else {
@@ -53,11 +38,12 @@ const wsLink = new WebSocketLink({
reconnect: true, reconnect: true,
lazy: true, lazy: true,
connectionParams: () => { connectionParams: () => {
console.log("WS Authorization") if (fb.idToken) {
console.log(authToken) return {}
} else {
return { return {
authorization: `Bearer ${authToken}`, authorization: `Bearer ${fb.idToken}`,
}
} }
}, },
}, },