diff --git a/src/renderer/lib/slippiBackend.ts b/src/renderer/lib/slippiBackend.ts index 46b08c766..9565c5fb8 100644 --- a/src/renderer/lib/slippiBackend.ts +++ b/src/renderer/lib/slippiBackend.ts @@ -57,13 +57,14 @@ const handleErrors = (errors: readonly GraphQLError[] | undefined) => { } }; -export async function fetchPlayKey(): Promise { +// The firebase ID token expires after 1 hour so we will refresh it for actions that require it. +async function refreshFirebaseAuth(): Promise { const user = firebase.auth().currentUser; if (!user) { - throw new Error("Failed to get play key. User is not logged in"); + throw new Error("User is not logged in."); } - const token = user ? await user.getIdToken() : ""; + const token = await user.getIdToken(); const authLink = new ApolloLink((operation, forward) => { // Use the setContext method to set the HTTP headers. @@ -78,6 +79,12 @@ export async function fetchPlayKey(): Promise { }); client.setLink(authLink.concat(httpLink)); + return user; +} + +export async function fetchPlayKey(): Promise { + const user = await refreshFirebaseAuth(); + const res = await client.query({ query: getUserKeyQuery, variables: { @@ -123,10 +130,8 @@ export async function deletePlayKey(): Promise { } export async function changeDisplayName(name: string) { - const user = firebase.auth().currentUser; - if (!user) { - throw new Error("Failed to change display name. User is not logged in"); - } + const user = await refreshFirebaseAuth(); + const res = await client.mutate({ mutation: renameUserMutation, variables: { fbUid: user.uid, displayName: name } }); handleErrors(res.errors); @@ -139,10 +144,7 @@ export async function changeDisplayName(name: string) { } export async function initNetplay(codeStart: string): Promise { - const user = firebase.auth().currentUser; - if (!user) { - throw new Error("Failed to set connect code. User is not logged in"); - } + await refreshFirebaseAuth(); const res = await client.mutate({ mutation: initNetplayMutation, variables: { codeStart } }); handleErrors(res.errors);