Getting a user's identity

V0.5.0 added principalId to the standard library allowing you to work with a shared function’s {caller} object, here’s an example retrieving an external caller’s id:

import PrincipalId "mo:stdlib/principalId";
import Hash "mo:stdlib/hash";
import Prim "mo:prim";

type Id = Hash.Hash;

actor {
    func getUserId(user: Principal) : Id { 
        return PrincipalId.hash(user);
    };

    public shared x func main() {
        Prim.debugPrint(debug_show( getUserId(x.caller) ));
    };
};
6 Likes

Thank you for this example Ori. I was actually trying to get the external caller id another way but this is much easier.

4 Likes