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:

  1. Create a ConsentFlow contract defining what data you want to read or write.

  2. Generate a URL for users to view and consent to this contract.

  3. Simulate placing a consent button on a webpage.

  4. Handle the redirect after a user consents to capture their DID (Decentralized Identifier).

  5. Read data from the contract for that consenting user.

  6. Send a new credential to that user through the contract.

Prerequisites:

  1. LearnCard SDK Initialized: You'll need an active learnCard instance connected to the network. We'll call it networkLearnCard.

  2. Service Profile Created: Your application needs its own profile on the LearnCard Network to act as the owner of the ConsentFlow contract.

  3. Basic Understanding: Familiarity with DIDs and Verifiable Credentials (VCs) will be helpful.

  4. 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.

โœจ Good to know:

  • read and write permissions are structured by personal data fields and credentials (grouped by categories).

  • You can mark items as required: true or required: false.

  • The redirectUrl is crucial for getting the user back to your application with their consent information.

  • Supported credential categories include: Achievement, ID, Learning History, Work History, Social Badge, Membership, Accomplishment, Accommodation, Family, Course.

Step 1.2: Create the Contract

Now, use the LearnCard SDK to publish this contract definition to the LearnCard Network.


Now that your contract exists, users need a way to view and consent to it.

The LearnCard platform provides a standard URL for users to interact with ConsentFlow contracts.

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.

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.

Note on reading specific user data: The getConsentFlowData retrieves all consented data for the contract. To get data for a specific user, you'd typically filter the results by the user's DID, or your backend might record the link between the termsUri (returned when a user consents) and the user's DID for more direct lookups via getConsentFlowTransactions or similar. The most direct way to get data for a specific DID for your contracts is often getConsentFlowDataForDid, then filtering the results for the relevant contractUri if that method returns data across multiple contracts. For this tutorial, we're simplifying.

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.


Summary & Next Steps

You've now learned the end-to-end process of:

  1. Creating a ConsentFlow contract.

  2. Generating a URL for users to provide consent.

  3. Handling the redirect to capture the consenting user's DID.

  4. Reading data the user consented to share.

  5. 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:

Check out our Core Concept pages on Consent Contracts, User Consent & Terms, and Accessing Consented Data for more details.

Last updated

Was this helpful?