Internet Identity in Production: Sessions, Principals, Custom Domains, and Backend Authorization

·

Authentication on ICP looks deceptively simple: Add an Internet Identity login button, receive an identity, pass it to an agent, and you have authenticated users. That is enough for a demo. It is not enough for a production application. The difficult problems begin afterward: determining what the backend should actually trust, keeping sessions alive without accidentally creating permanent credentials, understanding why the same person can appear under two different principals, moving an application onto a custom domain without creating duplicate accounts, and separating authentication from authorization. This guide builds a modern Internet Identity integration using the current ICP JavaScript SDK, then deals with the problems I would want solved before allowing that authentication system to protect real application state. The model: your backend does not receive a "login" Internet Identity is not a conventional username/password service. When a user authenticates, Internet Identity provides a delegation that allows a temporary identity to make authenticated ICP calls. Your frontend gives that identity to an HttpAgent , and calls signed by the agent arrive at your canister with the authenticated principal as caller . Conceptually: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff User > Internet Identity (w/ delegation) > Browser identity > HttpAgent (makes signed canister calls) > Backend function with ({ caller }) : Principal The backend does not need to ask the frontend "Which user are you?" ICP already authenticated the call. That distinction should drive the entire authorization architecture. Internet Identity currently supports passkeys as well as OpenID accounts including Google, Apple and Microsoft, while still presenting the application with an ICP identity rather than requiring the application to build separate OAuth account systems. It's one of the major contributions to the IC. Use the current packages A large number of ICP tutorials still begin with: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff npm install @dfinity/auth-client @dfinity/agent I would not start a new application that way. The current packages are: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff npm install @icp-sdk/auth @icp-sdk/core and the imports are: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff import { AuthClient } from "@icp-sdk/auth/client" ; import { Actor, HttpAgent } from "@icp-sdk/core/agent" ; As of August 28, 2026, @icp-sdk/auth is at 8.0.3 , published August 25. The old @dfinity/auth-client package is deprecated, and ICP JS SDK v5 completed the migration of the old @dfinity/* agent packages into @icp-sdk/core . This is exactly the sort of ecosystem change that makes authentication code copied from an older tutorial age poorly. Enable Internet Identity locally If you are using the current icp CLI, configure your local environment in icp.yaml : Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff networks: - name: local mode: managed ii: true The ii: true setting causes the local environment to provide Internet Identity automatically. You can then run your normal local environment rather than maintaining a special authentication setup. For local development, the current default II address is: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff http://id.ai.localhost:8000 while production authentication uses: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff https://id.ai/authorize The current SDK documentation uses these new II endpoints, so I would avoid copying older examples centered around identity.ic0.app unless maintaining an existing application. Create one authentication service I prefer putting authentication lifecycle management in one place rather than allowing components to create their own AuthClient and agents. For example: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff import { AuthClient } from "@icp-sdk/auth/client" ; let authClient : AuthClient | null = null ; export async function getAuthClient () { if (!authClient) { authClient = await AuthClient . create (); } return authClient; } Now initialization, login, logout and identity retrieval all operate on the same client. A React component should not need to understand delegation storage just to display "Sign in" Restore the session before rendering authenticated UI One of the first UX mistakes I see is treating authentication as something that only happens when the login button is clicked. AuthClient persists its authentication state. On application startup, check it: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff export async function restoreSession () { const authClient = await getAuthClient (); const authenticated = await authClient. isAuthenticated (); if (!authenticated) { return null ; } return authClient. getIdentity (); } Then your application initialization becomes approximately: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff const identity = await restoreSession (); if (identity) { // construct authenticated actor } else { // construct anonymous actor } This avoids the common flash where a returning user briefly appears logged out while the frontend initializes. More importantly, the browser's restored identity is still only the frontend's credential for making authenticated calls. It is not your application's authorization database. Login A simple login helper can look like: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff export async function login () { const authClient = await getAuthClient (); await new Promise < void >( (resolve, reject) => { authClient. login ({ identityProvider : getIdentityProvider (), maxTimeToLive : BigInt ( 8 * 60 * 60 ) * BigInt ( 1_000_000_000 ), onSuccess : () => resolve (), onError : (error) => reject ( new Error (error ?? "Internet Identity login failed" )), }); }); return authClient. getIdentity (); } and: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff function getIdentityProvider () { const host = window . location . hostname ; const local = host === "localhost" || host === "127.0.0.1" || host. endsWith ( ".localhost" ); return local ? "http://id.ai.localhost:8000" : "https://id.ai/authorize" ; } The TTL above is eight hours . That choice deserves more thought than it usually receives. Delegation lifetime is a security decision Internet Identity does not hand your web application the user's permanent master key. It delegates authority to a temporary browser identity. The current ICP authentication guidance recommends an eight-hour delegation by default and permits applications to request up to 30 days. You could therefore request: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff const THIRTY_DAYS = BigInt ( 30 * 24 * 60 * 60 ) * BigInt ( 1_000_000_000 ); and produce a much more persistent login. That is not automatically better. A long-lived delegation means a compromised browser session remains useful for longer. I would choose the TTL according to what the application can do. A read-heavy social application may reasonably favor longer sessions. An administrative interface controlling valuable canisters should probably favor substantially shorter sessions. Do not choose 30 days simply because the API allows 30 days. Create the agent from the authenticated identity After login: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff import { HttpAgent } from "@icp-sdk/core/agent" ; export async function createAgent () { const authClient = await getAuthClient (); const identity = authClient. getIdentity (); return await HttpAgent . create ({ host : "https://icp-api.io" , identity, }); } The current ICP client documentation uses HttpAgent.create() with the authenticated identity passed directly into the agent. Your generated actor can then use that agent: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff const actor = Actor . createActor (idlFactory, { agent, canisterId, }); Calls through this actor are now signed by the delegated identity. Do not send the principal as an authorization parameter This is wrong: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff public func deleteAccount (user : Principal ) : async () { accounts. remove (user); }; if the intention is to "delete my account"... The frontend controls user . Therefore, anyone can call: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff deleteAccount (victimPrincipal) Instead: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff public shared ({ caller }) func deleteMyAccount () : async () { accounts. remove (caller); }; The caller comes from ICP's authenticated message. That is the identity the backend should trust. If a user is allowed to act on another user's account, that should be an explicit authorization rule rather than something inferred because the frontend supplied a principal. Assume that the frontend has been compromised outside of the authentications given. Reject anonymous callers explicitly Every ICP call has an identity, including unauthenticated calls. Anonymous callers use: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff 2vxsx-fae So a simple Motoko helper is useful: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff import Principal "mo:core/Principal" ; import Runtime "mo:core/Runtime" ; func requireAuthenticated ( caller : Principal ) { if ( Principal . isAnonymous (caller)) { Runtime . trap ( "Authentication required" ); }; }; Then: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff public shared ({ caller }) func updateBio (bio : Text ) : async () { requireAuthenticated (caller); // update caller's profile }; This is much stronger than hiding the button in React. Frontend permission checks improve UX. Backend permission checks provide security. Authentication is not authorization Suppose the application has moderators. This is not enough: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Jav