Create a ConsentFlow
Tutorial: Implementing a Basic ConsentFlow
What is a ConsentFlow? A ConsentFlow is a powerful mechanism in LearnCard that allows your application or service (as an "Issuer" or "Contract Owner") to request permission from users ("Holders") to access certain parts of their data or to write new information (like credentials) to their profile. It's all based on explicit user consent, ensuring transparency and user control.
What you'll accomplish in this tutorial:
Create a ConsentFlow contract defining what data you want to read or write.
Generate a URL for users to view and consent to this contract.
Simulate placing a consent button on a webpage.
Handle the redirect after a user consents to capture their DID (Decentralized Identifier).
Read data from the contract for that consenting user.
Send a new credential to that user through the contract.
Prerequisites:
LearnCard SDK Initialized: You'll need an active
learnCardinstance connected to the network. We'll call itnetworkLearnCard.Service Profile Created: Your application needs its own profile on the LearnCard Network to act as the owner of the ConsentFlow contract.
Basic Understanding: Familiarity with DIDs and Verifiable Credentials (VCs) will be helpful.
Web Environment: You'll need a way to simulate a user clicking a link and your application handling a redirect (e.g., a simple HTML page and some client-side JavaScript for testing).
Part 1: Creating Your ConsentFlow Contract
This contract will define what permissions your application is requesting from users.
Step 1.1: Define Your Contract's Terms
The contract specifies what your app can read from a user's profile/wallet and what it can write to it, after they consent.
Step 1.2: Create the Contract
Now, use the LearnCard SDK to publish this contract definition to the LearnCard Network.
Action: Call this function. Keep the contractUri safe โ it's the unique identifier for your contract.
Part 2: Enabling User Consent on Your Website
Now that your contract exists, users need a way to view and consent to it.
Step 2.1: Construct the Consent URL
The LearnCard platform provides a standard URL for users to interact with ConsentFlow contracts.
Important: The returnTo URL must be http:// or https://.
Step 2.2: Add a Consent Button to Your Webpage
On your website or application, provide a button or link that directs the user to this userFacingConsentUrl.
When a user clicks this, they'll be taken to learncard.app to review your contract and give their consent.
Part 3: Handling the Redirect and Capturing the User's DID
After the user consents (or denies) on learncard.app, they will be redirected back to the redirectUrl you specified in your contract (or the returnTo URL in the consent link). The DID of the consenting user will be appended as a query parameter.
Step 3.1: Your Redirect Page (https://yourapp.com/consent-callback)
https://yourapp.com/consent-callback)This page in your application needs to be able to read URL query parameters.
Step 3.2: Extract the User's DID
Here's a simple client-side JavaScript example for your redirect page:
Now you have the DID of the user who consented! This is crucial for interacting with the data they agreed to share.
Part 4: Interacting with Consented Data
With the contractUri and the userDid of the consenter, your service (using its networkLearnCard instance) can now act on the consent.
Step 4.1: Reading Data Shared by the User
You can fetch all data shared by users for a specific contract. Then, you can find the record for the specific user using their DID.
Step 4.2: Sending a Credential to the User Through the Contract
Your contract might allow you to write credentials to users who have consented.
Important:
The
credential.typeand its category must match what your ConsentFlow contract allows forwritepermissions.The
boostUriparameter inwriteCredentialToContractlinks the issued credential to a "Boost," which can act as a template or define its category and display properties. Ensure this Boost exists and your service profile has permission to use it.
Summary & Next Steps
You've now learned the end-to-end process of:
Creating a ConsentFlow contract.
Generating a URL for users to provide consent.
Handling the redirect to capture the consenting user's DID.
Reading data the user consented to share.
Sending/Writing a new credential to the user through the contract.
This is a foundational flow for many powerful applications. From here, you can explore more advanced ConsentFlow features like:
Using Auto-Boosts to automatically issue credentials upon consent.
More complex data queries.
Check out our Core Concept pages on Consent Contracts, User Consent & Terms, and Accessing Consented Data for more details.
Last updated
Was this helpful?