I have created a library and template project that enables easy use of Internet Identity from Expo Web/iOS/Android.
Thanks to GitHub - krpeacock/ic-expo-mvp: MVP example of Expo making an IC mainnet call
This library and template also support connecting to Local Canisters from Expo Go.
Implementation Guide
You can implement Internet Identity authentication with these simple steps:
- Set up Authentication Provider: Configure the provider in your app entry file
// Set up IIIntegrationProvider in app/_layout.tsx
import { useIIIntegration, IIIntegrationProvider } from 'expo-ii-integration';
const auth = useIIIntegration({
localIPAddress: LOCAL_IP_ADDRESS,
dfxNetwork: ENV_VARS.DFX_NETWORK,
iiIntegrationCanisterId: ENV_VARS.CANISTER_ID_II_INTEGRATION,
iiCanisterId: ENV_VARS.CANISTER_ID_INTERNET_IDENTITY,
});
return <IIIntegrationProvider value={auth}>...</IIIntegrationProvider>;
- Implement Login Functionality: Add login feature with just a few lines
// Use login function in components/LogIn.tsx
import { useIIIntegrationContext } from 'expo-ii-integration';
const { login } = useIIIntegrationContext();
await login();
- Implement Logout Functionality: Add logout feature just as easily
// Use logout function in components/LogOut.tsx
import { useIIIntegrationContext } from 'expo-ii-integration';
const { logout } = useIIIntegrationContext();
await logout();
- Backend Integration: Call backend Canister using authenticated identity
// Call backend Canister in components/WhoAmI.tsx
import { useIIIntegrationContext } from 'expo-ii-integration';
import { createBackend } from '@/backend';
const { identity } = useIIIntegrationContext();
const backend = createBackend(identity);
await backend.whoami();