From afb241232b3b552d0426a036272fa2e684f65a4a Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 6 Jun 2022 15:26:45 +0200 Subject: [PATCH] fix: don't call /users/@me without token --- frontend/src/lib/store.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/frontend/src/lib/store.ts b/frontend/src/lib/store.ts index 3b88310..c041382 100644 --- a/frontend/src/lib/store.ts +++ b/frontend/src/lib/store.ts @@ -1,5 +1,6 @@ import axios from "axios"; import { atom, useRecoilState, useRecoilValue } from "recoil"; +import fetchAPI from "./fetch"; import { APIError, ErrorCode, MeUser } from "./types"; export const userState = atom({ @@ -12,16 +13,12 @@ async function getCurrentUser() { if (!token) return null; try { - const resp = await axios.get("/api/v1/users/@me"); - if (resp.status === 200) { - return resp.data as MeUser; - } - - // if we got a forbidden error, the token is invalid - if ((resp.data as APIError).code === ErrorCode.Forbidden) { + return await fetchAPI("/users/@me"); + } catch (e) { + if ((e as APIError).code === ErrorCode.Forbidden) { localStorage.removeItem("pronouns-token"); } - } catch (e) { + console.log("Error fetching /users/@me:", e); }