From f405f2bbe0d578b69bf7204f7fbbe3969d888728 Mon Sep 17 00:00:00 2001 From: Dunemask Date: Sun, 4 Feb 2024 20:10:35 -0700 Subject: [PATCH 1/2] [FIX] Enable Production Auth --- src/util/auth.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/util/auth.js b/src/util/auth.js index fe3138b..d16e9cc 100644 --- a/src/util/auth.js +++ b/src/util/auth.js @@ -2,6 +2,9 @@ import { useState, useContext, useEffect } from "react"; import { useSearchParams, useNavigate } from "react-router-dom"; import SettingsContext from "@mcl/settings"; +const tokenStorageName = "cairoAuthToken"; +const tokenQuery = "cairoAuthToken"; + const verifyAuth = (authToken) => fetch("/api/auth/verify", { headers: { Authorization: `Bearer ${authToken}` }, @@ -10,29 +13,34 @@ const verifyAuth = (authToken) => .catch(() => false); export function useCairoAuth() { - const { state: settings, updateSettings } = useContext(SettingsContext); - const [auth, setAuth] = useState(!!settings.cairoAuth); - const [searchParams] = useSearchParams(); - const nav = useNavigate(); + const [authToken, setAuthToken] = useState( + localStorage.getItem(tokenStorageName), + ); + const [auth, setAuth] = useState(false); + const [searchParams, setSearchParams] = useSearchParams(); useEffect(() => { - const webToken = searchParams.get("cairoAuthToken"); + if (!authToken) return; + verifyAuth(authToken).then((authorized) => { + if (!authorized) localStorage.removeItem(tokenStorageName); + setAuth(authorized); + }); + }, [authToken]); + + useEffect(() => { + const webToken = searchParams.get(tokenQuery); if (!webToken) return; - verifyAuth(webToken).then(setAuth); - updateSettings({ cairoAuth: webToken }); - nav("/"); + localStorage.setItem(tokenStorageName, webToken); + searchParams.delete(tokenQuery); + setAuthToken(webToken); + setSearchParams(searchParams); }, [searchParams]); - useEffect(() => { - verifyAuth(settings.cairoAuth).then(setAuth); - nav("/"); - }, [settings.cairoAuth]); - return auth; } export function getAuthTokenFromStorage() { - return JSON.parse(localStorage.getItem("settings")).cairoAuth; + return localStorage.getItem(tokenStorageName); } export function cairoAuthHeader() { From b33746ba8c7ce7a628fc57bbc0b3ce5006238359 Mon Sep 17 00:00:00 2001 From: Dunemask Date: Sun, 4 Feb 2024 20:10:58 -0700 Subject: [PATCH 2/2] [FIX] Enable Production Auth --- src/util/auth.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/util/auth.js b/src/util/auth.js index d16e9cc..f25a6bb 100644 --- a/src/util/auth.js +++ b/src/util/auth.js @@ -1,6 +1,5 @@ -import { useState, useContext, useEffect } from "react"; -import { useSearchParams, useNavigate } from "react-router-dom"; -import SettingsContext from "@mcl/settings"; +import { useState, useEffect } from "react"; +import { useSearchParams } from "react-router-dom"; const tokenStorageName = "cairoAuthToken"; const tokenQuery = "cairoAuthToken";