Assigning Usernames
This guide explains how to assign and unassign a username to an Account on Lens.
Assign a Username
To assign a Username to the logged-in Account, follow these steps.
You MUST be authenticated as Account Owner or Account Manager of the Account you want to assign a Username to.
First, list all usernames owned by the logged-in Account.
- TypeScript
- GraphQL
- React
Use the paginated fetchUsernames action to fetch the list of usernames owned by the logged-in Account address.
Get Owned Usernames
import { evmAddress } from "@lens-protocol/client";import { fetchUsernames } from "@lens-protocol/client/actions";
import { client } from "./client";
const result = await fetchUsernames(sessionClient, { filter: { owned: evmAddress("0x1234…") },});
if (result.isErr()) { return console.error(result.error);}
// items: Array<Username>const { items, pageInfo } = result.value;
See the Pagination guide for more information on how to handle paginated results.
Next, inspect the username.operations.canAssign field of the desired Username to determine whether the logged-in Account is allowed to assign the given Username. Some username namespaces may have restrictions on who can assign a Username.
Check Rules
switch (username.operations.canAssign.__typename) { case "NamespaceOperationValidationPassed": // Assignment is allowed break;
case "NamespaceOperationValidationFailed": // Assignment is not allowed console.log(username.operations.canAssign.reason); break;
case "NamespaceOperationValidationUnknown": // Validation outcome is unknown break;}
Where:
NamespaceOperationValidationPassed: The logged-in Account is allowed to assign the given Username.
NamespaceOperationValidationFailed: Assignment is not allowed. The reason field explains why, and unsatisfiedRules lists the unmet requirements.
NamespaceOperationValidationUnknown: The Namespace has one or more unknown rules requiring ad-hoc verification. The extraChecksRequired field provides the addresses and configurations of these rules.
Treat the NamespaceOperationValidationUnknown as failed unless you intend to support the specific rules. See Namespace Rules for more information.
Next, if allowed, assign the desired Username to the Account.
- TypeScript
- GraphQL
- React
Use the assignUsernameToAccount action to assign a Username.
- TypeScript
- GraphQL
- React
Finally, handle the result using the adapter for the library of your choice:
See the Transaction Lifecycle guide for more information on how to determine the status of the transaction.
Unassign a Username
To unassign a Username from the logged-in Account, follow these steps.
You MUST be authenticated as Account Owner or Account Manager of the Account that owns the Username you want to unassign.
First, inspect the username.operations.canUnassign field of the desired Username to determine whether the logged-in Account is allowed to unassign the given Username. Some username namespaces may have restrictions on who can unassign a Username.
Check Rules
switch (username.operations.canUnassign.__typename) { case "NamespaceOperationValidationPassed": // Unassignment is allowed break;
case "NamespaceOperationValidationFailed": // Unassignment is not allowed console.log(username.operations.canUnassign.reason); break;
case "NamespaceOperationValidationUnknown": // Validation outcome is unknown break;}
Where:
NamespaceOperationValidationPassed: The logged-in Account is allowed to unassign the given Username.
NamespaceOperationValidationFailed: Unassignment is not allowed. The reason field explains why, and unsatisfiedRules lists the unmet requirements.
NamespaceOperationValidationUnknown: The Namespace has one or more unknown rules requiring ad-hoc verification. The extraChecksRequired field provides the addresses and configurations of these rules.
Treat the NamespaceOperationValidationUnknown as failed unless you intend to support the specific rules. See Namespace Rules for more information.
Next, if allowed, unassign the Username.
- TypeScript
- GraphQL
- React
Use the unassignUsernameFromAccount to unassign a Username.
- TypeScript
- GraphQL
- React
Finally, handle the result using the adapter for the library of your choice:
See the Transaction Lifecycle guide for more information on how to determine the status of the transaction.