Issue with Anonymous principal

I posted a question earlier about another issue which was resolved but in the same code, I’m getting anonymous user principal, even if the user is logged in.
what am I missing here

Front end code (called right after login): icp_app/src/icp_app_frontend/src/App.jsx at main · salmanwaseem007/icp_app · GitHub
Backend code: icp_app/src/icp_app_backend/main.mo at main · salmanwaseem007/icp_app · GitHub

Front end

const _principal = identity.getPrincipal().toString();
const agent = new HttpAgent({ _principal });
let actor = await createActor(process.env.CANISTER_ID_ICP_APP_BACKEND, {
agent,
});
actor.getUserPrincipal(‘hello’).then(response => {
console.log(‘getUserPrincipal’, response);
});

Backend

public shared query ({ caller }) func getUserPrincipal(name : Text) : async Text {
print(debug_show ("logged in user: ") # Principal.toText(caller));
return "Hello, " # name # "! " # "Your PrincipalId is: " # Principal.toText(caller);
};

This is not how you can create agent with identity!

const agent = new HttpAgent({ _principal });

You need to set agent like this

const agent = new HttpAgent({ identity })

I highly recommend you to take a look at the @ic-reactor/react

2 Likes