Documentation
Feedback
Guides
VTEX IO Apps

VTEX IO Apps
Advanced components
Safedata
Official extension
Version: 1.3.1
Latest version: 1.3.1

This app is no longer maintained by VTEX. This means it will not be updated, and support and maintenance are no longer provided.

The Safedata app provides a configurable middleware to retrieve and save Master Data v1 and v2 information directly on the front-end or through another back-end application using shopper token authentication, rather than application keys.

This app works by acting as an validation layer on top of Master Data API to ensure data is being queried only by the user who owns it. The app makes the decision if the data can be returned or edited based on who is trying to access it, so it will only allow access to the information of the shopper who is logged in.

Relevant use cases include forms, such as warranty information submission, newsletter subscription, buyer's club registration or reseller registration.

Installation

You can install Safedata through the VTEX App Store or by running the following command on VTEX IO CLI:


_10
vtex install vtex.safedata

Upon installation, Safedata routes become instantly available to use with default entities CL (Client) and AD (Address). Other entities can be set up through app settings.

How it works

The process is based on the CL entity, which is used as a guide to identify the user. First, we reach out to VTEX ID to confirm the StoreUserAuthToken is valid and get the user email. Then we retrieve only the necessary fields on the CL entity to ensure the user is retrieving or modifying only documents that belongs to them.

This is done through a field comparison between CL and the entity is being queried. For instance, when searching for documents of the AD entity, we compare their userId to the id found in the CL entity, and the action is only allowed if they match.

The comparison fields must be searchable on the Master Data fields configuration. However, they do not have to be public. We do not recommend making any fields public.

Setting up entities

The basic configurations for CL and AD entities are set by default, as illustrated below, but can be changed by using the app settings interface.

{"base64":"  ","img":{"width":1259,"height":822,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":51697,"url":"https://github-production-user-asset-6210df.s3.amazonaws.com/77292838/316919642-3b009aef-bbe7-4e6c-b0e8-e673e86846d2.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240417%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240417T133423Z&X-Amz-Expires=300&X-Amz-Signature=75f0015ce81331cae0c27f4fe6011352726a2b3808f295ff406429067b01207b&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=489000048"}}

To enable other Master Data entities to work with Safedata, configure them on the app's settings as explained below:

  1. On VTEX Admin, go to Apps > Extensions Hub > App Management.

  2. Search for Safedata.

  3. Click Settings.

  4. Click + Add more.

  5. Fill in the required information:

    • Entity acronym on Master Data
    • Field to match on current entity
    • Field to match on CL entity
    • Allow creating objects anonymously?
  6. Click Save.

You can delete entity configurations by clicking the trash icon in their row.

Querying Master Data information

To query Master Data information using Safedata, you need to replace api/dataentities for api/io/safedata in Master Data API v1 and Master Data API v2 routes.

For example, if you need to query the AD entity by a specific addressName, the request would be: GET https://apiexamples.myvtex.com/api/io/safedata/AD/search?_where=addressName=12345

Supported routes

Safedata supports the following routes:

ActionOriginal routeSafedata route
Get documentGET /api/dataentities/{dataEntityName}/documents/{documentId}GET /api/io/safedata/{dataEntityName}/documents/{documentId}
Search documentsGET /api/dataentities/{dataEntityName}/searchGET /api/io/safedata/{dataEntityName}/search
Create documentPOST /api/dataentities/{dataEntityName}/documentsPOST /api/io/safedata/{dataEntityName}/documents
Create document with custom ID or update entire documentPUT /api/dataentities/{dataEntityName}/documents/{documentId}PUT /api/io/safedata/{dataEntityName}/documents/{documentId}
Delete documentDELETE /api/dataentities/{dataEntityName}/documents/{documentId}DELETE /api/io/safedata/{dataEntityName}/documents/{documentId}
Update partial documentPATCH /api/dataentities/{dataEntityName}/documents/{documentId}PATCH /api/io/safedata/{dataEntityName}/documents/{documentId}

All underscore query parameters are supported (_where, _fields, _schema, and so on).

Expected responses

If the user is logged out and tries to fetch information that does not belong to them, the middleware returns a status 403. It is possible to verify these requests through DevTools. Another status that the middleware returns are:

ResponseMeaning
401 UnauthorizedNo token informed (not logged in) or invalid token.
403 ForbiddenOperation not allowed. The user is trying to retrieve or edit data that is not their own.
404 Not FoundOccurs when the user is not found or when a document search does not return data with the specified filter (Safedata always filters the search to return only documents belonging to the shoppers who is logged in).
200 OKSuccessful request.

GraphQL interface

Safedata also provides a patchDocument mutation in GraphQL which enables React components to create or update documents in Master Data.

You can test it yourself using the GraphQL IDE.

{"base64":"  ","img":{"width":2154,"height":1357,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":259750,"url":"https://user-images.githubusercontent.com/1629129/127065235-fcf682d2-4b15-42d2-8d9b-b7b2df7d1d81.png"}}

Example mutation

This example demonstrates how to use the patchDocument mutation to create a document with a new email value:


_10
mutation patchDocument($document: DocumentInput) {
_10
patchDocument(entity: "CL", document:$document){
_10
id
_10
href
_10
documentId
_10
result
_10
}
_10
}

Input Fields

  • entity (String!): The entity type of the document to update.
  • document (DocumentInput!): The document fields to update.

DocumentInput Fields

  • fields ([DocumentFieldInput!]!): An array of fields to update in the document, with a key/value pair representing each field.

DocumentFieldInput Fields

  • key (String!): The key of the field to update.
  • value (String!): The new value of the field to update.

Example query variables


_12
{
_12
"document": {
_12
"document": {
_12
"fields": [
_12
{
_12
"key": "email",
_12
"value": "test@mail.com"
_12
}
_12
]
_12
}
_12
}
_12
}

Example result


_10
{
_10
"data": {
_10
"patchDocument": {
_10
"id": "CL-25bbdecd-eb92-11ee-8452-122c0cad37a5",
_10
"href": "http://api.vtex.com/api/dataentities/CL/documents/25bbdecd-eb92-11ee-8452-122c0cad37a5?an=apiexamples",
_10
"documentId": "25bbdecd-eb92-11ee-8452-122c0cad37a5",
_10
"result": "ok"
_10
}
_10
}
_10
}

Working with custom checkout fields

When a shopper is making their first purchase, the CL entity may be created preemptively if the store has specific customizations to include new fields in the checkout process, i.e., birth date. This might cause issues since Safedata forbids customers to change personal data when they are not logged in.

To avoid this problem, you can add the query parameter _orderFormId so Safedata can ensure the customer can safely change their information during their first checkout. See the following example:

PATCH /safedata/CL/documents?_orderFormId=7217c9c7413142dd93e3b715f95cd03d


_10
{
_10
"email": "my_email@domain.com",
_10
"birthDate": "1990-10-11T00:00:00"
_10
}

When this request is called for the first time the user does not exist, so it is allowed to be created anonymously.

In subsequent calls, Safedata checks the _orderFormId to ensure the user was created in this checkout session, so they can update their information without asking for credentials.

This allows users to update their personal information during the checkout process without logging in only during the first purchase.

Once the first purchase is finished, a complete profile is generated, and in order to update this information again the user has to log in.

Only the PATCH /api/io/safedata/{dataEntityName}/documents/{documentId} route supports the _orderFormId parameter, and it only works with entities that contain an email field in their schema.

See also
VTEX App Store
VTEX IO Apps