ic-use-internet-identity
Internet Identity is an authentication service running on the Internet Computer. It allows users to create an identity that can be used to authenticate with canisters (smart contracts) running on the Internet Computer.
ic-use-internet-identity
is a hook that makes it easy to integrate Internet Identity into your React application. It provides a simple interface for logging in and out with the Internet Identity service.
![downloads](https://img.shields.io/npm/dw/ic-use-internet-identity)
Features
- Cached Identity: The identity is cached in local storage and restored on page load. This allows the user to stay logged in even if the page is refreshed.
- Login progress: State varibles are provided to indicate whether the user is logged in, logging in, or logged out.
- Works with ic-use-actor: Plays nicely with ic-use-actor that provides easy access to canister methods.
Table of Contents
Installation
pnpm install ic-use-internet-identity
The hook also requires the following @dfinity/x
packages to be installed with a version of at least 2.1.2
:
pnpm install @dfinity/agent @dfinity/auth-client @dfinity/identity
Usage
[!TIP]
For a complete example, see the ic-use-internet-identity-demo demo project.
To use ic-use-internet-identity
in your React application, follow these steps:
1. Setup the InternetIdentityProvider
component
Wrap your application's root component with InternetIdentityProvider
to provide all child components access to the identity context.
import { InternetIdentityProvider } from "ic-use-internet-identity";
import React from "react";
import ReactDOM from "react-dom/client";
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<InternetIdentityProvider>
<App />
</InternetIdentityProvider>
</React.StrictMode>
);
[!TIP] > InternetIdentityProvider
defaults to using the main Internet Identity instance running on https://identity.ic0.app
. If you want to use a local instance of the Internet Identity, override the II_URL
environment variable with the URL of the local instance.
Example for Vite, using the vite-plugin-environment plugin:
import environment from "vite-plugin-environment";
process.env.II_URL =
process.env.DFX_NETWORK === "local"
? `http://${process.env.CANISTER_ID_INTERNET_IDENTIY}.localhost:4943`
: `https://identity.ic0.app`;
export default defineConfig({
plugins: [
environment(["II_URL"]),
],
});
2. Connect the login()
function to a button
Calling login()
opens up the Internet Identity service in a new window where the user is asked to sign in. Once signed in, the window closes and the identity is stored in local storage. The identity is then available in the identity
context variable.
Use the loginStatus
state variable to track the status of the login process. The loginStatus
can be one of the following values: idle
, logging-in
, success
, or error
.
import { useInternetIdentity } from "ic-use-internet-identity";
export function LoginButton() {
const { login, loginStatus } = useInternetIdentity();
const disabled = loginStatus === "logging-in" || loginStatus === "success";
const text = loginStatus === "logging-in" ? "Logging in..." : "Login";
return (
<button onClick={login} disabled={disabled}>
{text}
</button>
);
}
3. Use the identity
context variable to access the identity
The identity
context variable contains the identity of the currently logged in user. The identity is available after successfully loading the identity from local storage or completing the login process.
The preferred way to use the identity is to connect it to the ic-use-actor hook. The hook provides a typed interface to the canister methods as well as interceptor functions for handling errors etc.
import { ReactNode } from "react";
import {
ActorProvider,
createActorContext,
createUseActorHook,
} from "ic-use-actor";
import {
canisterId,
idlFactory,
} from "path-to/your-service/index";
import { _SERVICE } from "path-to/your-service.did";
import { useInternetIdentity } from "ic-use-internet-identity";
const actorContext = createActorContext<_SERVICE>();
export const useActor = createUseActorHook<_SERVICE>(actorContext);
eexport default function Actors({ children }: { children: ReactNode }) {
const { identity } = useInternetIdentity();
return (
<ActorProvider<_SERVICE>
canisterId={canisterId}
context={actorContext}
identity={identity}
idlFactory={idlFactory}
>
{children}
</ActorProvider>
);
}
InternetIdentityProvider props
{
createOptions?: AuthClientCreateOptions;
loginOptions?: LoginOptions;
children: ReactNode;
}
LoginOptions
export type LoginOptions = {
maxTimeToLive?: bigint;
allowPinAuthentication?: boolean;
derivationOrigin?: string | URL;
windowOpenerFeatures?: string;
customValues?: Record<string, unknown>;
};
useInternetIdentity interface
export type InternetIdentityContextType = {
isInitializing: boolean;
login: () => Promise<void>;
loginStatus: LoginStatus;
isLoggingIn: boolean;
isLoginError: boolean;
isLoginSuccess: boolean;
isLoginIdle: boolean;
loginError?: Error;
clear: () => Promise<void>;
identity?: Identity;
};
Updates
See the CHANGELOG for details on updates.
Author
Contributing
Contributions are welcome. Please submit your pull requests or open issues to propose changes or report bugs.
License
This project is licensed under the MIT License. See the LICENSE file for more details.