auth changes

This commit is contained in:
PhilReact 2024-10-25 08:45:39 +03:00
parent b29906af84
commit 02ebd5d87d
2 changed files with 42 additions and 6 deletions

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "Qortal", "name": "Qortal",
"version": "2.1.0", "version": "2.2.0",
"icons": { "icons": {
"16": "qort.png", "16": "qort.png",
"32": "qort.png", "32": "qort.png",

View File

@ -52,7 +52,7 @@ export const NotAuthenticated = ({
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 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
@ -105,11 +105,18 @@ export const NotAuthenticated = ({
currentNodeRef.current = currentNode currentNodeRef.current = currentNode
}, [currentNode]) }, [currentNode])
useEffect(()=> {
hasLocalNodeRef.current = hasLocalNode
}, [hasLocalNode])
const validateApiKey = useCallback(async (key) => { 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 isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
if(isLocalKey && !hasLocalNodeRef.current && !fromStartUp){
throw new Error('Please turn on your local node')
}
const isCurrentNodeLocal = cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391"; const isCurrentNodeLocal = cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
if(isLocalKey && !isCurrentNodeLocal) { if(isLocalKey && !isCurrentNodeLocal) {
setIsValidApiKey(false); setIsValidApiKey(false);
@ -145,6 +152,9 @@ export const NotAuthenticated = ({
handleSetGlobalApikey(payload); handleSetGlobalApikey(payload);
setIsValidApiKey(true); setIsValidApiKey(true);
setUseLocalNode(true); setUseLocalNode(true);
if(!fromStartUp){
setApiKey(payload)
}
} }
} }
); );
@ -162,15 +172,16 @@ export const NotAuthenticated = ({
setUseLocalNode(false); setUseLocalNode(false);
setInfoSnack({ setInfoSnack({
type: "error", type: "error",
message: "Select a valid apikey", message: error?.message || "Select a valid apikey",
}); });
setOpenSnack(true);
console.error("Error validating API key:", error); console.error("Error validating API key:", error);
} }
}, []); }, []);
useEffect(() => { useEffect(() => {
if (apiKey) { if (apiKey) {
validateApiKey(apiKey); validateApiKey(apiKey, true);
} }
}, [apiKey]); }, [apiKey]);
@ -282,7 +293,7 @@ export const NotAuthenticated = ({
visibility: !useLocalNode && 'hidden' visibility: !useLocalNode && 'hidden'
}} }}
> >
{"Using node: "} {apiKey?.url} {"Using node: "} {currentNode?.url}
</Typography> </Typography>
<> <>
<Spacer height="15px" /> <Spacer height="15px" />
@ -356,6 +367,10 @@ export const NotAuthenticated = ({
onChange={handleFileChangeApiKey} // File input handler onChange={handleFileChangeApiKey} // File input handler
/> />
</Button> </Button>
<Typography sx={{
fontSize: '12px',
visibility: importedApiKey ? 'visible' : 'hidden'
}}>{`api key : ${importedApiKey}`}</Typography>
<Spacer height="5px" /> <Spacer height="5px" />
@ -438,6 +453,17 @@ export const NotAuthenticated = ({
}); });
setMode("list"); setMode("list");
setShow(false); setShow(false);
setUseLocalNode(false);
chrome?.runtime?.sendMessage(
{ action: "setApiKey", payload:null },
(response) => {
if (response) {
setApiKey(null);
handleSetGlobalApikey(null);
}
}
);
}} }}
variant="contained" variant="contained"
> >
@ -483,6 +509,16 @@ export const NotAuthenticated = ({
setShow(false); setShow(false);
setIsValidApiKey(false); setIsValidApiKey(false);
setUseLocalNode(false); setUseLocalNode(false);
chrome?.runtime?.sendMessage(
{ action: "setApiKey", payload:null },
(response) => {
if (response) {
setApiKey(null);
handleSetGlobalApikey(null);
}
}
);
}} }}
variant="contained" variant="contained"
> >