Get unique ID on the frontend

I am looking for a way to get a unique ID for the user after they sign-in using the normal IC method.

There is a local cookie ic-identity which changes every time a user logs in and logs out.
Is there any way to get a unique id on the frontend from their identity, that is the same each time a user logs in?

Tried this

public shared (msg) func sendId(): async Text {
  return Principal.toText(msg.caller)
};

But then get an error

Reject text: Canister ryjl3-tyaaa-aaaaa-aaaba-cai trapped explicitly: IDL error: byte read out of buffer

Any ideas?

After I cleared that error I get

Canister ryjl3-tyaaa-aaaaa-aaaba-cai trapped explicitly: IDL error: unexpected IDL type when parsing Principal

I was able to resolve this by removing the .dfx directory and rebuilding everything.

Great question! The identity you get back each time is a DelegationIdentity, which will be different every time, but will resolve to a stable principal.

If you are using the auth-client package, you can access the principal with a logged-in auth-client using authClient.getIdentity().getPrincipal().toText();

1 Like

This is exactly what I was looking for!! :partying_face: