[FIX] Enable Production Auth
This commit is contained in:
parent
40b3a42c73
commit
f405f2bbe0
1 changed files with 22 additions and 14 deletions
|
@ -2,6 +2,9 @@ import { useState, useContext, useEffect } from "react";
|
||||||
import { useSearchParams, useNavigate } from "react-router-dom";
|
import { useSearchParams, useNavigate } from "react-router-dom";
|
||||||
import SettingsContext from "@mcl/settings";
|
import SettingsContext from "@mcl/settings";
|
||||||
|
|
||||||
|
const tokenStorageName = "cairoAuthToken";
|
||||||
|
const tokenQuery = "cairoAuthToken";
|
||||||
|
|
||||||
const verifyAuth = (authToken) =>
|
const verifyAuth = (authToken) =>
|
||||||
fetch("/api/auth/verify", {
|
fetch("/api/auth/verify", {
|
||||||
headers: { Authorization: `Bearer ${authToken}` },
|
headers: { Authorization: `Bearer ${authToken}` },
|
||||||
|
@ -10,29 +13,34 @@ const verifyAuth = (authToken) =>
|
||||||
.catch(() => false);
|
.catch(() => false);
|
||||||
|
|
||||||
export function useCairoAuth() {
|
export function useCairoAuth() {
|
||||||
const { state: settings, updateSettings } = useContext(SettingsContext);
|
const [authToken, setAuthToken] = useState(
|
||||||
const [auth, setAuth] = useState(!!settings.cairoAuth);
|
localStorage.getItem(tokenStorageName),
|
||||||
const [searchParams] = useSearchParams();
|
);
|
||||||
const nav = useNavigate();
|
const [auth, setAuth] = useState(false);
|
||||||
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
|
|
||||||
useEffect(() => {
|
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;
|
if (!webToken) return;
|
||||||
verifyAuth(webToken).then(setAuth);
|
localStorage.setItem(tokenStorageName, webToken);
|
||||||
updateSettings({ cairoAuth: webToken });
|
searchParams.delete(tokenQuery);
|
||||||
nav("/");
|
setAuthToken(webToken);
|
||||||
|
setSearchParams(searchParams);
|
||||||
}, [searchParams]);
|
}, [searchParams]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
verifyAuth(settings.cairoAuth).then(setAuth);
|
|
||||||
nav("/");
|
|
||||||
}, [settings.cairoAuth]);
|
|
||||||
|
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAuthTokenFromStorage() {
|
export function getAuthTokenFromStorage() {
|
||||||
return JSON.parse(localStorage.getItem("settings")).cairoAuth;
|
return localStorage.getItem(tokenStorageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cairoAuthHeader() {
|
export function cairoAuthHeader() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue