Quick Start
This guide offers a simple example of how to use the @icp-sdk/auth package to authenticate a user with Internet Identity on an Internet Computer web app.
In a web application, you can use the package in this way:
import { AuthClient } from '@icp-sdk/auth/client';import { HttpAgent } from '@icp-sdk/core/agent';
const network = 'ic'; // typically, this value is read from the environment (e.g. process.env.DFX_NETWORK)const identityProvider = network === 'ic' ? 'https://id.ai/authorize' // Mainnet : 'http://id.ai.localhost:8000'; // default name mapping set by icp-cli when ii is enabled
const authClient = new AuthClient({ identityProvider });
// Check for an existing session (synchronous)if (authClient.isAuthenticated()) { const identity = await authClient.getIdentity(); console.log('Restored session:', identity.getPrincipal().toString());}
const canisterId = Principal.fromText('uqqxf-5h777-77774-qaaaa-cai');const agent = await HttpAgent.create({ host: 'https://icp-api.io',});
try { await authClient.login();} catch (error) { console.error('Login failed:', error);}
const identity = await authClient.getIdentity();agent.replaceIdentity(identity);
// this call will be authenticatedawait agent.call(canisterId, { methodName: 'greet', arg: IDL.encode([IDL.Text], ['world']),});
// later in your appawait authClient.logout();Next Steps
Section titled “Next Steps”Check out the Integrating Internet Identity guide for a more detailed guide on how to integrate Internet Identity into your web app.
For a full example, check out the Who Am I example.