Skip to content

Commit

Permalink
fix(slippiBackend): refresh firebase auth
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilNarayana committed Jan 17, 2022
1 parent fe466e7 commit b7b5eff
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/renderer/lib/slippiBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ const handleErrors = (errors: readonly GraphQLError[] | undefined) => {
}
};

export async function fetchPlayKey(): Promise<PlayKey> {
// The firebase ID token expires after 1 hour so we will refresh it for actions that require it.
async function refreshFirebaseAuth(): Promise<firebase.User> {
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.
Expand All @@ -78,6 +79,12 @@ export async function fetchPlayKey(): Promise<PlayKey> {
});
client.setLink(authLink.concat(httpLink));

return user;
}

export async function fetchPlayKey(): Promise<PlayKey> {
const user = await refreshFirebaseAuth();

const res = await client.query({
query: getUserKeyQuery,
variables: {
Expand Down Expand Up @@ -123,10 +130,8 @@ export async function deletePlayKey(): Promise<void> {
}

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);
Expand All @@ -139,10 +144,7 @@ export async function changeDisplayName(name: string) {
}

export async function initNetplay(codeStart: string): Promise<void> {
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);
Expand Down

0 comments on commit b7b5eff

Please sign in to comment.