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 { 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
*/
const authLink = setContext((_, { headers }) => {
if (authToken) {
if (fb.idToken) {
return {
headers: {
...headers,
authorization: `Bearer ${authToken}`,
authorization: `Bearer ${fb.idToken}`,
},
}
} else {
@@ -53,11 +38,12 @@ const wsLink = new WebSocketLink({
reconnect: true,
lazy: true,
connectionParams: () => {
console.log("WS Authorization")
console.log(authToken)
return {
authorization: `Bearer ${authToken}`,
if (fb.idToken) {
return {}
} else {
return {
authorization: `Bearer ${fb.idToken}`,
}
}
},
},