# Problems with cross referencing principals

**URL:** <https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926>\
**Category:** Developers\
**Created:** [July 15, 2021, 6:53pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926 "2021-07-15T18:53:37Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 15, 2021, 6:53pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/1 "2021-07-15T18:53:37Z")

</div>

Hi,

I’m having the following issue;

I have the following function that will add a new member to the group based on the principal (`msg.caller`);

```auto
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`…

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/1/1b8fed59b325130c3e3b6856a252c6701e642ebd.png)

What am i doing wrong?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [July 15, 2021, 6:56pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/2 "2021-07-15T18:56:23Z")

</div>

How are you setting up and passing the identity client-side?

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 15, 2021, 7:00pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/3 "2021-07-15T19:00:06Z")

</div>

```auto
let authClient: AuthClient;

async function createAuthClient() {
		try {
			const newAuthClient = await AuthClient.create();
			setState(prevState => ({ ...prevState, authClientReady: true }));
			authClient = newAuthClient;
			console.log('created');
		} catch (error) {
			console.log(error);
			setState(prevState => ({ ...prevState, authClientReady: false }));
		}
	}

function getIdentity() {
		return authClient.getIdentity();
	}

```

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [July 15, 2021, 7:03pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/4 "2021-07-15T19:03:32Z")

</div>

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.

> <https://github.com/krpeacock/auth-client-demo/blob/main/src/frontend/src/index.ts#L30>

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 15, 2021, 7:07pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/5 "2021-07-15T19:07:59Z")

</div>

Ahh i didn’t know that, i thought it was the other way around, so if you wanted to use the `AnonymousIdentity` you needed to specify this.

Let me check if this works 👍 thanks in advance

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 15, 2021, 8:15pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/6 "2021-07-15T20:15:18Z")

</div>

> [@anon74414410](#):
>
> [https://github.com/krpeacock/auth-client-demo/blob/main/src/frontend/src/index.ts#L30](https://github.com/krpeacock/auth-client-demo/blob/main/src/frontend/src/index.ts#L30)

Hmm when i set it i get a `TypeError: id.getPrincipal is not a function` error 🤔

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [July 15, 2021, 8:18pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/7 "2021-07-15T20:18:34Z")

</div>

oh yeah, I’ve encountered that error before. I think it had to do with the canisterId not being set properly during the actor setup.

I should make an issue for that

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 15, 2021, 8:20pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/8 "2021-07-15T20:20:23Z")

</div>

I now have something like this

```auto
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
});
}

```

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [July 15, 2021, 8:35pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/9 "2021-07-15T20:35:18Z")

</div>

I’m not seeing anything you’re doing wrong per se - although I don’t think the return types you’re using will work correctly.

The way I would do this would be to update to 0.7.7 and use the exported `createActor` method, passed like this:

```auto
const identity = authClient.getIdentity();
const profileActor = createActor(profile_id, {
  agentOptions: { identity: authClient?.getIdentity() },
});

```

You’ll need to update `webpack.config.js` and `package.json` to match the new `dfx new` output, but I’m still working on migration docs

```auto
DFX_VERSION="0.7.6" sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"

```

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 15, 2021, 8:55pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/10 "2021-07-15T20:55:54Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/3/33cc84708653d23c202dd984c50baaa6c26088e6.png)

```auto
import { Identity } from '@dfinity/agent';
import { createActor as createProfileActor, canisterId as ProfileId } from 'dfx-generated/ProfileController';
import { createActor as createGroupActor, canisterId as GroupId } from 'dfx-generated/GroupController';

let identity: Identity = JSON.parse(localStorage.getItem('identity')!);

export function profileController() {
	return createProfileActor(ProfileId, {
		agentOptions: {
			host: 'http://localhost:8000',
			identity
		}
	});
}

export function groupController() {
	return createGroupActor(GroupId, {
		agentOptions: {
			host: 'http://localhost:8000',
			identity
		}
	});
}

```

tsconfig

```auto
{
	"compilerOptions": {
		"target": "ESNext",
		"lib": ["DOM", "DOM.Iterable", "ESNext"],
		"types": ["vite/client"],
		"allowJs": true,
		"skipLibCheck": true,
		"esModuleInterop": false,
		"allowSyntheticDefaultImports": true,
		"strict": true,
		"forceConsistentCasingInFileNames": true,
		"module": "ESNext",
		"moduleResolution": "Node",
		"resolveJsonModule": true,
		"isolatedModules": true,
		"noEmit": true,
		"jsx": "react",
		"baseUrl": "./",
		"paths": {
			"src/*": ["./src/*"],
			"dfx-generated/ProfileController": [".dfx/local/canisters/ProfileController"],
			"dfx-generated/GroupController": [".dfx/local/canisters/GroupController"]
		}
	},
	"include": ["./src"]
}

```

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [July 15, 2021, 9:00pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/11 "2021-07-15T21:00:26Z")

</div>

Okay, for vite, you will want to do the following:

1. remove the `prebuild` job from `package.json`
2. replace the `process.env` variables in the generated code with **`import.meta.env.`**
3. Follow this guide to handle environment variables in vite for `NODE_ENV` and `<canister_name_uppercase>_CANISTER_ID`  
[Env Variables and Modes | Vite](https://vitejs.dev/guide/env-and-mode.html#intellisense)

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 15, 2021, 9:08pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/12 "2021-07-15T21:08:18Z")

</div>

Thanks! Will check back tomorrow 👍🏻

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 16, 2021, 12:01pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/13 "2021-07-16T12:01:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stephenandrews](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/stephenandrews/32/1803_2.png) [@stephenandrews](https://forum.dfinity.org/u/stephenandrews)\
**Post date:** [July 16, 2021, 1:57pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/14 "2021-07-16T13:57:52Z")

</div>

What was your issue with 0.7.2? Your code looked OK

```auto
let identity: Identity = JSON.parse(localStorage.getItem('identity')!);

```

Can you output identity to ensure it is a valid Identity?

---

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 16, 2021, 5:35pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/15 "2021-07-16T17:35:26Z")

</div>

The 3d line is `console.log(identity)`;

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/b/bd57f7cbe40f476cb824114c7b3c5410b983d822.png)  
and the last log is the error i am getting

---

<div class="post-metadata">

**Author:** ![stephenandrews](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/stephenandrews/32/1803_2.png) [@stephenandrews](https://forum.dfinity.org/u/stephenandrews)\
**Post date:** [July 17, 2021, 3:04pm UTC](https://forum.dfinity.org/t/problems-with-cross-referencing-principals/5926/16 "2021-07-17T15:04:58Z")

</div>

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()`
