From e7dd67deaa3cc72403b6095983efc3fb4e57081b Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Thu, 13 May 2021 22:42:04 -0400 Subject: [PATCH] Update race condition with Apollo getting ID Token and BackendUserInfo --- helpers/apollo.ts | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/helpers/apollo.ts b/helpers/apollo.ts index 195bbac26..74ada4b09 100644 --- a/helpers/apollo.ts +++ b/helpers/apollo.ts @@ -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}`, + } } }, },