Custom Authentication allows dApp owners to provide PKPs (Programmable Key Pairs) to their users without requiring them to understand blockchain technology or manage private keys. Instead of forcing users to learn new authentication methods, you can leverage your existing authentication systems (OAuth, APIs, databases) while providing them with powerful web3 capabilities.This demonstrates the complete dApp-centric custom authentication flow from both perspectives: the Site Owner who sets up the system and the User who interacts with it.
Generate a unique authentication method type for your dApp using the dApp name. This creates a secure identifier that will be used for all PKP minting and validation.
Copy
// Generate unique auth method type for dAppimport { utils as litUtils } from "@lit-protocol/lit-client";const authMethodConfig = litUtils.generateUniqueAuthMethodType({ uniqueDappName: "stellar-network-mrcsjy",});console.log("Auth Method Type (hex):", authMethodConfig.hex);console.log("Auth Method Type (bigint):", authMethodConfig.bigint);
2
Create and Pin Validation Lit Action
Create the validation logic as a Lit Action and pin it to IPFS for immutable validation. This code will run on Lit nodes to validate user authentication attempts.
🔍 IPFS Visibility Required: The IPFS CID must be publicly accessible via the Lit Explorer. If the CID isn’t visible on explorer.litprotocol.com, the Lit nodes won’t be able to fetch and execute your validation logic.
3
Mint PKPs for Users
Mint PKPs for your users using the custom auth method type and validation CID. Each user gets their own unique PKP tied to your dApp’s authentication system.
Copy
import { utils as litUtils } from "@lit-protocol/lit-client";// Mint PKP for each user, assuming that's what you want to dofor (const userId of ["alice", "bob"]) { const authData = litUtils.generateAuthData({ uniqueDappName: "stellar-network-mrcsjy", uniqueAuthMethodType: authMethodConfig.bigint, userId: userId, }); const { pkpData } = await litClient.mintWithCustomAuth({ account: siteOwnerAccount, authData: authData, scope: "sign-anything", validationIpfsCid: "your-validation-cid-here", }); // Store PKP info for user database.users[userId].pkpPublicKey = pkpData.data.pubkey;}
How users interact with the dApp to authenticate and use their PKP
1
Login to dApp and Get PKP Inf
User logs into the dApp frontend and retrieves their pre-minted PKP information from the dApp’s backend. The dApp provides the PKP public key and validation details.
The following code is served as an example. It is not part of the SDK.
Copy
// User login and PKP retrieval const userDashboard = dappFrontend.login('alice', password); const userPkpInfo = userDashboard.getMyPkpInfo(); console.log('User PKP Public Key:', userPkpInfo.pkpPublicKey); console.log('Validation CID:', userPkpInfo.validationCid);
2
Generate Custom Auth Context
Create a custom auth context using the user’s PKP and the dApp’s validation IPFS CID. The Lit Action will validate the user’s credentials against the dApp’s authentication logic.