mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-04-28 05:47:52 +00:00
fix saving of apikeys
This commit is contained in:
parent
cc92b7d9bd
commit
ef5e68d0a9
102
src/App.tsx
102
src/App.tsx
@ -400,6 +400,11 @@ function App() {
|
||||
const [isOpenSendQort, setIsOpenSendQort] = useState(false);
|
||||
const [isOpenSendQortSuccess, setIsOpenSendQortSuccess] = useState(false);
|
||||
const [rootHeight, setRootHeight] = useState("100%");
|
||||
const [currentNode, setCurrentNode] = useState({
|
||||
url: "http://127.0.0.1:12391",
|
||||
});
|
||||
const [useLocalNode, setUseLocalNode] = useState(false);
|
||||
|
||||
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
|
||||
const [showSeed, setShowSeed] = useState(false)
|
||||
const [creationStep, setCreationStep] = useState(1)
|
||||
@ -516,7 +521,9 @@ function App() {
|
||||
globalApiKey = key;
|
||||
};
|
||||
useEffect(() => {
|
||||
window
|
||||
try {
|
||||
setIsLoading(true);
|
||||
window
|
||||
.sendMessage("getApiKey")
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
@ -529,7 +536,36 @@ function App() {
|
||||
"Failed to get API key:",
|
||||
error?.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
}).finally(()=> {
|
||||
window
|
||||
.sendMessage("getWalletInfo")
|
||||
.then((response) => {
|
||||
if (response && response?.walletInfo) {
|
||||
setRawWallet(response?.walletInfo);
|
||||
if (
|
||||
holdRefExtState.current === "web-app-request-payment" ||
|
||||
holdRefExtState.current === "web-app-request-connection" ||
|
||||
holdRefExtState.current === "web-app-request-buy-order"
|
||||
)
|
||||
return;
|
||||
if (response?.hasKeyPair) {
|
||||
setExtstate("authenticated");
|
||||
} else {
|
||||
setExtstate("wallet-dropped");
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to get wallet info:", error);
|
||||
});
|
||||
})
|
||||
} catch (error) {
|
||||
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
|
||||
}
|
||||
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
if (extState) {
|
||||
@ -627,8 +663,6 @@ function App() {
|
||||
setdecryptedWallet(null);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
|
||||
error = e;
|
||||
}
|
||||
},
|
||||
});
|
||||
@ -848,36 +882,6 @@ function App() {
|
||||
// REMOVED FOR MOBILE APP
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
window
|
||||
.sendMessage("getWalletInfo")
|
||||
.then((response) => {
|
||||
if (response && response?.walletInfo) {
|
||||
setRawWallet(response?.walletInfo);
|
||||
if (
|
||||
holdRefExtState.current === "web-app-request-payment" ||
|
||||
holdRefExtState.current === "web-app-request-connection" ||
|
||||
holdRefExtState.current === "web-app-request-buy-order"
|
||||
)
|
||||
return;
|
||||
if (response?.hasKeyPair) {
|
||||
setExtstate("authenticated");
|
||||
} else {
|
||||
setExtstate("wallet-dropped");
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to get wallet info:", error);
|
||||
});
|
||||
} catch (error) {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const getUserInfo = useCallback(async (useTimer?: boolean) => {
|
||||
try {
|
||||
@ -1712,6 +1716,10 @@ function App() {
|
||||
globalApiKey={globalApiKey}
|
||||
setApiKey={setApiKey}
|
||||
handleSetGlobalApikey={handleSetGlobalApikey}
|
||||
currentNode={currentNode}
|
||||
setCurrentNode={setCurrentNode}
|
||||
setUseLocalNode={setUseLocalNode}
|
||||
useLocalNode={useLocalNode}
|
||||
/>
|
||||
)}
|
||||
{/* {extState !== "not-authenticated" && (
|
||||
@ -2467,7 +2475,31 @@ function App() {
|
||||
}}
|
||||
ref={passwordRef}
|
||||
/>
|
||||
<Spacer height="20px" />
|
||||
{useLocalNode ? (
|
||||
<>
|
||||
<Spacer height="20px" />
|
||||
<Typography
|
||||
sx={{
|
||||
fontSize: "12px",
|
||||
}}
|
||||
>
|
||||
{"Using node: "} {currentNode?.url}
|
||||
</Typography>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Spacer height="20px" />
|
||||
<Typography
|
||||
sx={{
|
||||
fontSize: "12px",
|
||||
}}
|
||||
>
|
||||
{"Using gateway"}
|
||||
</Typography>
|
||||
</>
|
||||
)}
|
||||
|
||||
<Spacer height="20px" />
|
||||
<CustomButton onClick={authenticateWallet}>
|
||||
Authenticate
|
||||
</CustomButton>
|
||||
|
@ -22,13 +22,19 @@ import Info from "../assets/svgs/Info.svg";
|
||||
import HelpIcon from '@mui/icons-material/Help';
|
||||
import { CustomizedSnackbars } from "../components/Snackbar/Snackbar";
|
||||
import { set } from "lodash";
|
||||
import { cleanUrl, isUsingLocal } from "../background";
|
||||
import { cleanUrl, gateways, isUsingLocal } from "../background";
|
||||
import { GlobalContext } from "../App";
|
||||
|
||||
const manifestData = {
|
||||
version: "0.5.0",
|
||||
};
|
||||
|
||||
|
||||
function removeTrailingSlash(url) {
|
||||
return url.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
|
||||
export const NotAuthenticated = ({
|
||||
getRootProps,
|
||||
getInputProps,
|
||||
@ -38,18 +44,22 @@ export const NotAuthenticated = ({
|
||||
setApiKey,
|
||||
globalApiKey,
|
||||
handleSetGlobalApikey,
|
||||
currentNode,
|
||||
setCurrentNode,
|
||||
useLocalNode,
|
||||
setUseLocalNode
|
||||
}) => {
|
||||
const [isValidApiKey, setIsValidApiKey] = useState<boolean | null>(null);
|
||||
const [hasLocalNode, setHasLocalNode] = useState<boolean | null>(null);
|
||||
const [useLocalNode, setUseLocalNode] = useState(false);
|
||||
// const [useLocalNode, setUseLocalNode] = useState(false);
|
||||
const [openSnack, setOpenSnack] = React.useState(false);
|
||||
const [infoSnack, setInfoSnack] = React.useState(null);
|
||||
const [show, setShow] = React.useState(false);
|
||||
const [mode, setMode] = React.useState("list");
|
||||
const [customNodes, setCustomNodes] = React.useState(null);
|
||||
const [currentNode, setCurrentNode] = React.useState({
|
||||
url: "http://127.0.0.1:12391",
|
||||
});
|
||||
// const [currentNode, setCurrentNode] = React.useState({
|
||||
// url: "http://127.0.0.1:12391",
|
||||
// });
|
||||
const [importedApiKey, setImportedApiKey] = React.useState(null);
|
||||
//add and edit states
|
||||
const [url, setUrl] = React.useState("http://");
|
||||
@ -70,6 +80,34 @@ export const NotAuthenticated = ({
|
||||
const text = e.target.result; // Get the file content
|
||||
|
||||
setImportedApiKey(text); // Store the file content in the state
|
||||
if(customNodes){
|
||||
setCustomNodes((prev)=> {
|
||||
const copyPrev = [...prev]
|
||||
const findLocalIndex = copyPrev?.findIndex((item)=> item?.url === 'http://127.0.0.1:12391')
|
||||
if(findLocalIndex === -1){
|
||||
copyPrev.unshift({
|
||||
url: "http://127.0.0.1:12391",
|
||||
apikey: text
|
||||
})
|
||||
} else {
|
||||
copyPrev[findLocalIndex] = {
|
||||
url: "http://127.0.0.1:12391",
|
||||
apikey: text
|
||||
}
|
||||
}
|
||||
window
|
||||
.sendMessage("setCustomNodes", copyPrev)
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Failed to set custom nodes:",
|
||||
error.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
return copyPrev
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
reader.readAsText(file); // Read the file as text
|
||||
}
|
||||
@ -105,11 +143,16 @@ export const NotAuthenticated = ({
|
||||
window
|
||||
.sendMessage("getCustomNodesFromStorage")
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
|
||||
setCustomNodes(response || []);
|
||||
window.electronAPI.setAllowedDomains(response?.map((node)=> node.url))
|
||||
|
||||
}
|
||||
if(Array.isArray(response)){
|
||||
const findLocal = response?.find((item)=> item?.url === 'http://127.0.0.1:12391')
|
||||
if(findLocal && findLocal?.apikey){
|
||||
setImportedApiKey(findLocal?.apikey)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
@ -130,14 +173,42 @@ export const NotAuthenticated = ({
|
||||
hasLocalNodeRef.current = hasLocalNode;
|
||||
}, [hasLocalNode]);
|
||||
|
||||
|
||||
|
||||
const validateApiKey = useCallback(async (key, fromStartUp) => {
|
||||
try {
|
||||
const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
|
||||
if(fromStartUp && key?.url && key?.apikey && !isLocalKey && !gateways.some(gateway => apiKey?.url?.includes(gateway))){
|
||||
setCurrentNode({
|
||||
url: key?.url,
|
||||
apikey: key?.apikey,
|
||||
});
|
||||
const url = `${key?.url}/admin/apikey/test`;
|
||||
const response = await fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
accept: "text/plain",
|
||||
"X-API-KEY": key?.apikey, // Include the API key here
|
||||
},
|
||||
});
|
||||
|
||||
// Assuming the response is in plain text and will be 'true' or 'false'
|
||||
const data = await response.text();
|
||||
if (data === "true") {
|
||||
setIsValidApiKey(true);
|
||||
setUseLocalNode(true);
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
if (!currentNodeRef.current) return;
|
||||
const stillHasLocal = await checkIfUserHasLocalNode()
|
||||
const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
|
||||
|
||||
if (isLocalKey && !stillHasLocal && !fromStartUp) {
|
||||
throw new Error("Please turn on your local node");
|
||||
}
|
||||
//check custom nodes
|
||||
// !gateways.some(gateway => apiKey?.url?.includes(gateway))
|
||||
const isCurrentNodeLocal =
|
||||
cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
|
||||
if (isLocalKey && !isCurrentNodeLocal) {
|
||||
@ -240,12 +311,12 @@ export const NotAuthenticated = ({
|
||||
let nodes = [...(myNodes || [])];
|
||||
if (customNodeToSaveIndex !== null) {
|
||||
nodes.splice(customNodeToSaveIndex, 1, {
|
||||
url,
|
||||
url: removeTrailingSlash(url),
|
||||
apikey: customApikey,
|
||||
});
|
||||
} else if (url && customApikey) {
|
||||
nodes.push({
|
||||
url,
|
||||
url: removeTrailingSlash(url),
|
||||
apikey: customApikey,
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user