[FIX] Enable Correct Production Hooks (#13)
Co-authored-by: Dunemask <dunemask@gmail.com> Reviewed-on: https://gitea.dunemask.dev/elysium/minecluster/pulls/13
This commit is contained in:
parent
40b3a42c73
commit
1eaa7ff5a5
1 changed files with 24 additions and 17 deletions
|
@ -1,6 +1,8 @@
|
||||||
import { useState, useContext, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useSearchParams, useNavigate } from "react-router-dom";
|
import { useSearchParams } from "react-router-dom";
|
||||||
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", {
|
||||||
|
@ -10,29 +12,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