In Magento 2 B2B (Business-to-Business) environments, ensuring email uniqueness is essential when registering a company, assigning company users, or creating admin roles. Magento provides GraphQL queries to validate email availability for each use case.
In this tutorial, we’ll explore the following GraphQL queries:
isCompanyEmailAvailable
isCompanyUserEmailAvailable
isCompanyAdminEmailAvailable
All queries require a valid customer token and return a boolean value indicating whether the given email is available for the requested action.
Prerequisite: Generate Customer Token
Before using any of these queries, you need to generate a customer token via GraphQL:
mutation {
generateCustomerToken(
email: "johndoe@example.com"
password: "CustomerPassword123"
) {
token
}
}
Use this token as the Authorization
header for the upcoming queries:
Authorization: Bearer <customer-token>
Check Email Availability for Company Registration
Query: isCompanyEmailAvailable
This query checks if the provided email can be used to register a new company. It returns false
if the email already belongs to an existing company or customer.
Syntax
query{
isCompanyEmailAvailable(email: "roni_cost@example.com"){
is_email_available
}
}
Result
{
"data": {
"isCompanyEmailAvailable": {
"is_email_available": true
}
}
}
Check Email Availability for Company User
Query: isCompanyUserEmailAvailable
Use this query to check whether an email can be used to add a new user to a company. It returns false
if the email already belongs to a customer or company admin.
Syntax
{
isCompanyUserEmailAvailable(email: "roni_cost@example.com") {
is_email_available
}
}
Output
{
"data": {
"isCompanyUserEmailAvailable": {
"is_email_available": false
}
}
}
Check Email Availability for Company Admin
Query: isCompanyAdminEmailAvailable
This query checks if the given email is available to be used as a Company Administrator. Returns false
if it already belongs to a customer or another company admin.
Syntax
query {
isCompanyAdminEmailAvailable(email: "roni_cost@example.com") {
is_email_available
}
}
Output
{
"data": {
"isCompanyAdminEmailAvailable": {
"is_email_available": false
}
}
}
With these GraphQL queries, validating email availability for different roles in Magento 2 B2B becomes straightforward and efficient. Whether you’re registering a company, assigning users, or setting up admins — you can ensure data integrity and avoid conflicts right from the frontend.
You may also like,
How to Create a Customer Account Using Magento 2 GraphQL
How to Check Email Availability in Magento 2 Using GraphQL (2.4.7+)
How to Generate a Customer Token Using Magento 2 GraphQL
Leave a Comment