Local First Auth Client Library
The `local-first-auth` library provides a client-side implementation of the Local First Auth specification, enabling web applications to integrate authentication without relying on backend servers, passwords, or third-party providers. Currently at version 2.2.1, it focuses on providing a lightweight, DID-based (Decentralized Identifier, specifically `did:key`) authentication flow where user profiles and cryptographic keys are generated and stored locally on the user's device. This approach is ideal for temporary or in-person community applications like meetups, social clubs, or game nights, offering a simple 3-step onboarding process. A key differentiator is its serverless nature and focus on local data storage, which simplifies development for specific use cases. However, users must be aware that clearing browser data will result in the loss of their account keys, making it unsuitable for applications requiring persistent or multi-device account access. The library offers both React components and vanilla JavaScript functions for integration, shipping with TypeScript types.
Common errors
-
TypeError: (0 , _localFirstAuth_react__WEBPACK_IMPORTED_MODULE_0__.Onboarding) is not a function
cause Attempting to import the `Onboarding` React component using a CommonJS `require` syntax or incorrect named/default import in an environment expecting ESM.fixEnsure you are using ECMAScript Module (ESM) imports: `import { Onboarding } from 'local-first-auth/react'` and that your build tools (e.g., Webpack, Rollup) are configured to handle ESM properly. -
Module not found: Can't resolve 'react' in 'node_modules/local-first-auth/react'
cause The `local-first-auth` package has a `peerDependency` on React, meaning React must be installed by your project, but it's missing.fixInstall React in your project: `npm install react` (for React v18) or `npm install react@^19.0.0` (for React v19).
Warnings
- gotcha User authentication keys are stored locally (e.g., in IndexedDB or Local Storage) and will be permanently lost if the user clears their browsing data. This requires users to create a new account.
- gotcha This library is designed for single-device, temporary authentication and does not support persistent accounts across multiple devices or easy account recovery. It is fundamentally different from traditional email/password or OAuth systems.
Install
-
npm install local-first-auth -
yarn add local-first-auth -
pnpm add local-first-auth
Imports
- Onboarding
import Onboarding from 'local-first-auth/react'; const { Onboarding } = require('local-first-auth/react');import { Onboarding } from 'local-first-auth/react' - createOnboarding
import createOnboarding from 'local-first-auth'; const { createOnboarding } = require('local-first-auth');import { createOnboarding } from 'local-first-auth' - getProfileDetails
const { getProfileDetails } = require('local-first-auth');import { getProfileDetails } from 'local-first-auth'
Quickstart
import { useState } from 'react';
import { Onboarding } from 'local-first-auth/react';
function App() {
// Check if localFirstAuth is already initialized in the browser's window object.
// This helps determine if the user has an existing local-first profile.
const hasLocalFirstAuth = typeof window !== 'undefined' && window.localFirstAuth;
const [showOnboarding, setShowOnboarding] = useState(!hasLocalFirstAuth);
if (showOnboarding) {
return (
<Onboarding
// Callback function invoked upon successful completion of the onboarding process.
// The 'profile' object contains details like name, avatar, and DID.
onComplete={(profile) => {
console.log('Profile created:', profile);
// Hide the onboarding component after completion.
setShowOnboarding(false);
}}
// Customize the primary accent color of the onboarding UI.
customStyles={{ primaryColor: '#403B51' }}
/>
);
}
// Render your main application content once onboarding is complete or skipped.
return (
<div>
<h1>Welcome to Your App!</h1>
<p>Local-first authentication is active.</p>
{/* Your application's main components would go here */}
<button onClick={() => setShowOnboarding(true)}>Show Onboarding Again</button> {/* For demonstration */}
</div>
);
}