mirror of
https://github.com/Qortal/qortal-mobile.git
synced 2025-06-19 22:31:22 +00:00
fix local node auth
This commit is contained in:
parent
ab48491499
commit
7e2a1db62b
@ -23,20 +23,19 @@ import { set } from "lodash";
|
|||||||
import { cleanUrl, isUsingLocal } from "../background";
|
import { cleanUrl, isUsingLocal } from "../background";
|
||||||
|
|
||||||
export const manifestData = {
|
export const manifestData = {
|
||||||
version: '0.3.8'
|
version: "0.3.8",
|
||||||
}
|
};
|
||||||
|
|
||||||
export const NotAuthenticated = ({
|
export const NotAuthenticated = ({
|
||||||
getRootProps,
|
getRootProps,
|
||||||
getInputProps,
|
getInputProps,
|
||||||
setExtstate,
|
setExtstate,
|
||||||
|
|
||||||
|
|
||||||
apiKey,
|
apiKey,
|
||||||
setApiKey,
|
setApiKey,
|
||||||
globalApiKey,
|
globalApiKey,
|
||||||
handleSetGlobalApikey,
|
handleSetGlobalApikey,
|
||||||
handleFilePick
|
handleFilePick,
|
||||||
}) => {
|
}) => {
|
||||||
const [isValidApiKey, setIsValidApiKey] = useState<boolean | null>(null);
|
const [isValidApiKey, setIsValidApiKey] = useState<boolean | null>(null);
|
||||||
const [hasLocalNode, setHasLocalNode] = useState<boolean | null>(null);
|
const [hasLocalNode, setHasLocalNode] = useState<boolean | null>(null);
|
||||||
@ -55,9 +54,9 @@ export const NotAuthenticated = ({
|
|||||||
const [customApikey, setCustomApiKey] = React.useState("");
|
const [customApikey, setCustomApiKey] = React.useState("");
|
||||||
const [customNodeToSaveIndex, setCustomNodeToSaveIndex] =
|
const [customNodeToSaveIndex, setCustomNodeToSaveIndex] =
|
||||||
React.useState(null);
|
React.useState(null);
|
||||||
const importedApiKeyRef = useRef(null)
|
const importedApiKeyRef = useRef(null);
|
||||||
const currentNodeRef = useRef(null)
|
const currentNodeRef = useRef(null);
|
||||||
const hasLocalNodeRef = useRef(null)
|
const hasLocalNodeRef = useRef(null);
|
||||||
const isLocal = cleanUrl(currentNode?.url) === "127.0.0.1:12391";
|
const isLocal = cleanUrl(currentNode?.url) === "127.0.0.1:12391";
|
||||||
const handleFileChangeApiKey = (event) => {
|
const handleFileChangeApiKey = (event) => {
|
||||||
const file = event.target.files[0]; // Get the selected file
|
const file = event.target.files[0]; // Get the selected file
|
||||||
@ -72,7 +71,6 @@ export const NotAuthenticated = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const checkIfUserHasLocalNode = useCallback(async () => {
|
const checkIfUserHasLocalNode = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const url = `http://127.0.0.1:12391/admin/status`;
|
const url = `http://127.0.0.1:12391/admin/status`;
|
||||||
@ -85,8 +83,12 @@ export const NotAuthenticated = ({
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (data?.height) {
|
if (data?.height) {
|
||||||
setHasLocalNode(true);
|
setHasLocalNode(true);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} catch (error) {}
|
return false;
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -94,43 +96,47 @@ export const NotAuthenticated = ({
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.sendMessage("getCustomNodesFromStorage")
|
window
|
||||||
.then((response) => {
|
.sendMessage("getCustomNodesFromStorage")
|
||||||
if (response) {
|
.then((response) => {
|
||||||
setCustomNodes(response || []);
|
if (response) {
|
||||||
}
|
setCustomNodes(response || []);
|
||||||
})
|
}
|
||||||
.catch((error) => {
|
})
|
||||||
console.error("Failed to get custom nodes from storage:", error.message || "An error occurred");
|
.catch((error) => {
|
||||||
});
|
console.error(
|
||||||
|
"Failed to get custom nodes from storage:",
|
||||||
|
error.message || "An error occurred"
|
||||||
|
);
|
||||||
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(()=> {
|
useEffect(() => {
|
||||||
importedApiKeyRef.current = importedApiKey
|
importedApiKeyRef.current = importedApiKey;
|
||||||
}, [importedApiKey])
|
}, [importedApiKey]);
|
||||||
useEffect(()=> {
|
useEffect(() => {
|
||||||
currentNodeRef.current = currentNode
|
currentNodeRef.current = currentNode;
|
||||||
}, [currentNode])
|
}, [currentNode]);
|
||||||
|
|
||||||
useEffect(()=> {
|
useEffect(() => {
|
||||||
hasLocalNodeRef.current = hasLocalNode
|
hasLocalNodeRef.current = hasLocalNode;
|
||||||
}, [hasLocalNode])
|
}, [hasLocalNode]);
|
||||||
|
|
||||||
const validateApiKey = useCallback(async (key, fromStartUp) => {
|
const validateApiKey = useCallback(async (key, fromStartUp) => {
|
||||||
try {
|
try {
|
||||||
if(!currentNodeRef.current) return
|
if (!currentNodeRef.current) return;
|
||||||
const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
|
const stillHasLocal = await checkIfUserHasLocalNode();
|
||||||
if(isLocalKey && !hasLocalNodeRef.current && !fromStartUp){
|
const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
|
||||||
throw new Error('Please turn on your local node')
|
if (isLocalKey && !stillHasLocal && !fromStartUp) {
|
||||||
|
throw new Error("Please turn on your local node");
|
||||||
}
|
}
|
||||||
const isCurrentNodeLocal = cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
|
const isCurrentNodeLocal =
|
||||||
if(isLocalKey && !isCurrentNodeLocal) {
|
cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
|
||||||
setIsValidApiKey(false);
|
if (isLocalKey && !isCurrentNodeLocal) {
|
||||||
setUseLocalNode(false);
|
setIsValidApiKey(false);
|
||||||
return
|
setUseLocalNode(false);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
let payload = {};
|
let payload = {};
|
||||||
|
|
||||||
if (currentNodeRef.current?.url === "http://127.0.0.1:12391") {
|
if (currentNodeRef.current?.url === "http://127.0.0.1:12391") {
|
||||||
@ -138,7 +144,7 @@ export const NotAuthenticated = ({
|
|||||||
apikey: importedApiKeyRef.current || key?.apikey,
|
apikey: importedApiKeyRef.current || key?.apikey,
|
||||||
url: currentNodeRef.current?.url,
|
url: currentNodeRef.current?.url,
|
||||||
};
|
};
|
||||||
} else if(currentNodeRef.current) {
|
} else if (currentNodeRef.current) {
|
||||||
payload = currentNodeRef.current;
|
payload = currentNodeRef.current;
|
||||||
}
|
}
|
||||||
const url = `${payload?.url}/admin/apikey/test`;
|
const url = `${payload?.url}/admin/apikey/test`;
|
||||||
@ -153,21 +159,24 @@ export const NotAuthenticated = ({
|
|||||||
// Assuming the response is in plain text and will be 'true' or 'false'
|
// Assuming the response is in plain text and will be 'true' or 'false'
|
||||||
const data = await response.text();
|
const data = await response.text();
|
||||||
if (data === "true") {
|
if (data === "true") {
|
||||||
window.sendMessage("setApiKey", payload)
|
window
|
||||||
.then((response) => {
|
.sendMessage("setApiKey", payload)
|
||||||
if (response) {
|
.then((response) => {
|
||||||
handleSetGlobalApikey(payload);
|
if (response) {
|
||||||
setIsValidApiKey(true);
|
handleSetGlobalApikey(payload);
|
||||||
setUseLocalNode(true);
|
setIsValidApiKey(true);
|
||||||
if (!fromStartUp) {
|
setUseLocalNode(true);
|
||||||
setApiKey(payload);
|
if (!fromStartUp) {
|
||||||
|
setApiKey(payload);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
.catch((error) => {
|
||||||
.catch((error) => {
|
console.error(
|
||||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
"Failed to set API key:",
|
||||||
});
|
error.message || "An error occurred"
|
||||||
|
);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
setIsValidApiKey(false);
|
setIsValidApiKey(false);
|
||||||
setUseLocalNode(false);
|
setUseLocalNode(false);
|
||||||
@ -180,6 +189,26 @@ export const NotAuthenticated = ({
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
setIsValidApiKey(false);
|
setIsValidApiKey(false);
|
||||||
setUseLocalNode(false);
|
setUseLocalNode(false);
|
||||||
|
if(fromStartUp){
|
||||||
|
setCurrentNode({
|
||||||
|
url: "http://127.0.0.1:12391",
|
||||||
|
});
|
||||||
|
window
|
||||||
|
.sendMessage("setApiKey", null)
|
||||||
|
.then((response) => {
|
||||||
|
if (response) {
|
||||||
|
setApiKey(null);
|
||||||
|
handleSetGlobalApikey(null);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(
|
||||||
|
"Failed to set API key:",
|
||||||
|
error.message || "An error occurred"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return
|
||||||
|
}
|
||||||
setInfoSnack({
|
setInfoSnack({
|
||||||
type: "error",
|
type: "error",
|
||||||
message: error?.message || "Select a valid apikey",
|
message: error?.message || "Select a valid apikey",
|
||||||
@ -216,22 +245,24 @@ export const NotAuthenticated = ({
|
|||||||
setCustomNodes(nodes);
|
setCustomNodes(nodes);
|
||||||
setCustomNodeToSaveIndex(null);
|
setCustomNodeToSaveIndex(null);
|
||||||
if (!nodes) return;
|
if (!nodes) return;
|
||||||
window.sendMessage("setCustomNodes", nodes)
|
window
|
||||||
.then((response) => {
|
.sendMessage("setCustomNodes", nodes)
|
||||||
if (response) {
|
.then((response) => {
|
||||||
setMode("list");
|
if (response) {
|
||||||
setUrl("http://");
|
setMode("list");
|
||||||
setCustomApiKey("");
|
setUrl("http://");
|
||||||
// add alert if needed
|
setCustomApiKey("");
|
||||||
}
|
// add alert if needed
|
||||||
})
|
}
|
||||||
.catch((error) => {
|
})
|
||||||
console.error("Failed to set custom nodes:", error.message || "An error occurred");
|
.catch((error) => {
|
||||||
});
|
console.error(
|
||||||
|
"Failed to set custom nodes:",
|
||||||
|
error.message || "An error occurred"
|
||||||
|
);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Spacer height="35px" />
|
<Spacer height="35px" />
|
||||||
@ -263,10 +294,9 @@ export const NotAuthenticated = ({
|
|||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CustomButton onClick={()=> setExtstate('wallets')}>
|
<CustomButton onClick={() => setExtstate("wallets")}>
|
||||||
Wallets
|
Wallets
|
||||||
</CustomButton>
|
</CustomButton>
|
||||||
|
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<Spacer height="6px" />
|
<Spacer height="6px" />
|
||||||
@ -274,7 +304,7 @@ export const NotAuthenticated = ({
|
|||||||
sx={{
|
sx={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
gap: "10px",
|
gap: "10px",
|
||||||
alignItems: "center"
|
alignItems: "center",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CustomButton
|
<CustomButton
|
||||||
@ -284,19 +314,17 @@ export const NotAuthenticated = ({
|
|||||||
>
|
>
|
||||||
Create account
|
Create account
|
||||||
</CustomButton>
|
</CustomButton>
|
||||||
|
|
||||||
|
|
||||||
</Box>
|
</Box>
|
||||||
<Spacer height="15px" />
|
<Spacer height="15px" />
|
||||||
|
|
||||||
<Typography
|
<Typography
|
||||||
sx={{
|
sx={{
|
||||||
fontSize: "12px",
|
fontSize: "12px",
|
||||||
visibility: !useLocalNode && 'hidden'
|
visibility: !useLocalNode && "hidden",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{"Using node: "} {currentNode?.url}
|
{"Using node: "} {currentNode?.url}
|
||||||
</Typography>
|
</Typography>
|
||||||
<>
|
<>
|
||||||
<Spacer height="15px" />
|
<Spacer height="15px" />
|
||||||
<Box
|
<Box
|
||||||
@ -335,28 +363,30 @@ export const NotAuthenticated = ({
|
|||||||
validateApiKey(currentNode);
|
validateApiKey(currentNode);
|
||||||
} else {
|
} else {
|
||||||
setCurrentNode({
|
setCurrentNode({
|
||||||
url: "http://127.0.0.1:12391",
|
url: "http://127.0.0.1:12391",
|
||||||
|
});
|
||||||
|
setUseLocalNode(false);
|
||||||
|
window
|
||||||
|
.sendMessage("setApiKey", null)
|
||||||
|
.then((response) => {
|
||||||
|
if (response) {
|
||||||
|
setApiKey(null);
|
||||||
|
handleSetGlobalApikey(null);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
setUseLocalNode(false)
|
.catch((error) => {
|
||||||
window.sendMessage("setApiKey", null)
|
console.error(
|
||||||
.then((response) => {
|
"Failed to set API key:",
|
||||||
if (response) {
|
error.message || "An error occurred"
|
||||||
setApiKey(null);
|
);
|
||||||
handleSetGlobalApikey(null);
|
});
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}}
|
}}
|
||||||
disabled={false}
|
disabled={false}
|
||||||
defaultChecked
|
defaultChecked
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
label={`Use ${isLocal ? 'Local' : 'Custom'} Node`}
|
label={`Use ${isLocal ? "Local" : "Custom"} Node`}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
{currentNode?.url === "http://127.0.0.1:12391" && (
|
{currentNode?.url === "http://127.0.0.1:12391" && (
|
||||||
@ -370,31 +400,33 @@ export const NotAuthenticated = ({
|
|||||||
onChange={handleFileChangeApiKey} // File input handler
|
onChange={handleFileChangeApiKey} // File input handler
|
||||||
/>
|
/>
|
||||||
</Button>
|
</Button>
|
||||||
<Typography sx={{
|
<Typography
|
||||||
fontSize: '12px',
|
sx={{
|
||||||
visibility: importedApiKey ? 'visible' : 'hidden'
|
fontSize: "12px",
|
||||||
}}>{`api key : ${importedApiKey}`}</Typography>
|
visibility: importedApiKey ? "visible" : "hidden",
|
||||||
|
}}
|
||||||
|
>{`api key : ${importedApiKey}`}</Typography>
|
||||||
|
|
||||||
|
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setShow(true);
|
setShow(true);
|
||||||
}}
|
}}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
component="label"
|
component="label"
|
||||||
>
|
>
|
||||||
Choose custom node
|
Choose custom node
|
||||||
</Button>
|
</Button>
|
||||||
</>
|
</>
|
||||||
<Typography sx={{
|
<Typography
|
||||||
color: "white",
|
sx={{
|
||||||
fontSize: '12px'
|
color: "white",
|
||||||
}}>Build version: {manifestData?.version}</Typography>
|
fontSize: "12px",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Build version: {manifestData?.version}
|
||||||
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
</>
|
</>
|
||||||
<CustomizedSnackbars
|
<CustomizedSnackbars
|
||||||
@ -421,7 +453,6 @@ export const NotAuthenticated = ({
|
|||||||
flexDirection: "column",
|
flexDirection: "column",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
||||||
{mode === "list" && (
|
{mode === "list" && (
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
@ -463,17 +494,20 @@ export const NotAuthenticated = ({
|
|||||||
setMode("list");
|
setMode("list");
|
||||||
setShow(false);
|
setShow(false);
|
||||||
setUseLocalNode(false);
|
setUseLocalNode(false);
|
||||||
window.sendMessage("setApiKey", null)
|
window
|
||||||
.then((response) => {
|
.sendMessage("setApiKey", null)
|
||||||
if (response) {
|
.then((response) => {
|
||||||
setApiKey(null);
|
if (response) {
|
||||||
handleSetGlobalApikey(null);
|
setApiKey(null);
|
||||||
}
|
handleSetGlobalApikey(null);
|
||||||
})
|
}
|
||||||
.catch((error) => {
|
})
|
||||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
.catch((error) => {
|
||||||
});
|
console.error(
|
||||||
|
"Failed to set API key:",
|
||||||
|
error.message || "An error occurred"
|
||||||
|
);
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
>
|
>
|
||||||
@ -518,18 +552,21 @@ export const NotAuthenticated = ({
|
|||||||
setMode("list");
|
setMode("list");
|
||||||
setShow(false);
|
setShow(false);
|
||||||
setIsValidApiKey(false);
|
setIsValidApiKey(false);
|
||||||
setUseLocalNode(false);
|
setUseLocalNode(false);
|
||||||
window.sendMessage("setApiKey", null)
|
window
|
||||||
.then((response) => {
|
.sendMessage("setApiKey", null)
|
||||||
if (response) {
|
.then((response) => {
|
||||||
setApiKey(null);
|
if (response) {
|
||||||
handleSetGlobalApikey(null);
|
setApiKey(null);
|
||||||
}
|
handleSetGlobalApikey(null);
|
||||||
})
|
}
|
||||||
.catch((error) => {
|
})
|
||||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
.catch((error) => {
|
||||||
});
|
console.error(
|
||||||
|
"Failed to set API key:",
|
||||||
|
error.message || "An error occurred"
|
||||||
|
);
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
>
|
>
|
||||||
@ -554,7 +591,6 @@ export const NotAuthenticated = ({
|
|||||||
...(customNodes || []),
|
...(customNodes || []),
|
||||||
].filter((item) => item?.url !== node?.url);
|
].filter((item) => item?.url !== node?.url);
|
||||||
|
|
||||||
|
|
||||||
saveCustomNodes(nodesToSave);
|
saveCustomNodes(nodesToSave);
|
||||||
}}
|
}}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
@ -592,9 +628,7 @@ export const NotAuthenticated = ({
|
|||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
{mode === "list" && (
|
{mode === "list" && (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user