I’m having an issue with the Internet Identity authentication and actor set up with the “@dfinity/agent” package on my React app. It’s becoming an elusive issue as it only appears maybe once out of every 10 - 20 log in attempts. I’ll try to explain the issue the best I can and what I have tried. Maybe someone has ran into this before?
After logging in via Internet Identity the above error appears sometimes, I understand that either the public key or private key is not matching but I’m unsure how I can debug this further. My Agent flow looks something like:
I have a file named client.js
and in it I set up my actors with Actor.createActor
. For example this is how I do it:
export const startClient = async () => {
const canisterId = process.env.REACT_APP_CANISTER_ID;
const authClient = await AuthClient.create();
const identity = await authClient.getIdentity();
return Actor.createActor(CanisterIDL, {
agent: new HttpAgent({
identity,
host: ICP_API,
}),
canisterId: canisterId,
});
};
So when a user loads up my app Identity
is the anonymous identity at first, and then when they log in it’s set to the delegation identity to start making authenticated calls. Works most of the time.
My guesses here is that maybe the anonymous identity is cached as when I receive the agent error I mentioned previously and I comment out the Identity in my actor initialisation like below, the calls go through okay.:
export const startClient = async () => {
const canisterId = process.env.REACT_APP_CANISTER_ID;
const authClient = await AuthClient.create();
const identity = await authClient.getIdentity();
return Actor.createActor(CanisterIDL, {
agent: new HttpAgent({
// identity,
host: ICP_API,
}),
canisterId: canisterId,
});
};
I’ve tried this too and it doesn’t do anything:
export const startClient = async () => {
const canisterId = process.env.REACT_APP_CANISTER_ID;
const authClient = await AuthClient.create();
const identity = await authClient.getIdentity();
const actor = Actor.createActor(CanisterIDL, {
agent: new HttpAgent({
identity,
host: ICP_API,
}),
canisterId: canisterId,
});
const defaultAgent = Actor.agentOf(actor);
defaultAgent.replaceIdentity(identity);
return actor
};
But interestingly when I do this:
export const startClient = async () => {
const canisterId = process.env.REACT_APP_CANISTER_ID;
const authClient = await AuthClient.create();
const identity = await authClient.getIdentity();
const actor = Actor.createActor(CanisterIDL, {
agent: new HttpAgent({
identity,
host: ICP_API,
}),
canisterId: canisterId,
});
const defaultAgent = Actor.agentOf(actor);
defaultAgent.invalidateIdentity();
return actor
};
I get this error?
Is the only solution here to refresh the whole login? When the app is reloaded the error persists, only when you do a fresh logout and login it can disappear.
Im using "@dfinity/agent": "^1.3.0"
and "@dfinity/auth-client": "^1.3.0"