I have the following function that will add a new member to the group based on the principal (msg.caller);
public shared(msg) func join(groupId: Int) : async Result.Result<Bool, GroupError> {
var existing = groups.get(groupId);
switch (existing) {
case (?existing) {
let newGroup : Group = {
id = existing.id;
members = Array.append<Principal>(existing.members, [msg.caller]);
};
groups.put(groupId,newGroup);
#ok(true);
};
case (null) {
#err(#message("No Group Found"));
};
};
};
When i try to filter out the groups where the user is in the members array, i find that the added principal (msg.caller) is different then the getIdentity().getPrincipal() in the frontend
As seen in the image, the first principal is the one i get from getIdentity().getPrincipal() and the array i get from the group.members…
By default, your HttpAgent will be making all calls with an AnonymousIdentity. To have your actor use the identity, you’ll need to recreate the HttpAgent, passing the new identity as an option.
import { Actor, HttpAgent, HttpAgentOptions, Identity } from '@dfinity/agent';
import { idlFactory as profile_idl, canisterId as profile_id } from 'dfx-generated/ProfileController';
import { default as PROFILE_SERVICE } from './ProfileController';
import { idlFactory as group_idl, canisterId as group_id } from 'dfx-generated/GroupController';
import { default as GROUP_SERVICE } from './GroupController';
let identity: Identity = JSON.parse(localStorage.getItem('identity')!);
const agentOptions: HttpAgentOptions = {
// ONLY ENABLE THIS FOR LOCAL TESTING
host: 'http://localhost:8000',
identity
};
const agent = new HttpAgent(agentOptions);
// ONLY ENABLE THIS FOR LOCAL TESTING
agent.fetchRootKey();
export function profileController(): PROFILE_SERVICE {
return Actor.createActor(profile_idl, {
agent: agent,
canisterId: profile_id
});
}
export function groupController(): GROUP_SERVICE {
return Actor.createActor(group_idl, {
agent: agent,
canisterId: group_id
});
}
Hmm, will wait for the docs then, i just installed version 0.7.7 and it generates some other code, but got some reference errors, btw i’m using vite instead of webpack.
Do you have an example project or something and iss this new method stable?
Running into error after error, to the point i reverted everything. Think it’s better to wait until the migration is documented.
in the meantime do you have a solution to fix this with dfx version 0.7.2?
For future reference, this was resolved in private. The issue was storing the identity in localstorage as opposed to pulling it again via authClient.getIdentity()