This library provides a comprehensive JavaScript/Typescript interface for integrating with the Community of Trust (COT) platform, facilitating seamless interactions with its services.
- node.js for development
- npm for package management
To install the library, run the following command in your project directory:
npm i @trustedshops-public/cot-integration-library
Here is a basic example of how to use the library:
On the backend side:
import { Client } from '@trustedshops-public/cot-integration-library';
// Initialize the client with the required parameters once
const client = new Client(
<TSID>, // Trusted Shops ID - e.g. 'X1234567890123456789012345678901'
<CLIENT_ID>, // Client ID - e.g. 'cot-switch-X1234567890123456789012345678901'
<CLIENT_SECRET>, // Client Secret - e.g. '1234567890123456789012345678901234567890123456789012345678901234'
<AUTH_STORAGE_INSTANCE>, // It can be any storage option implementing AuthStorageInterface - e.g. new DatabaseAuthStorage()
<ENV> // Environment (optional) - dev, test, or prod, defaults to prod
);
// Set the cookie handler to handle cookies everytime before a request is made and handled
client.setCookieHandler(<COOKIE_HANDLER_INSTANCE>);
// Pass the actual url which the authentication has been started from to let authentication server verify the redirect uri e.g. 'https://www.example.com/shop'
client.setRedirectUri(<REDIRECT_URI>);
// Invoke handleCallback function to handle code coming from the authentication server
await client.handleCallback(authCode);
// Get anonymous consumer data for the current user
const consumerData = await client.getAnonymousConsumerData();
On the frontend side, place the following code in your HTML file where you want the widget to appear:
<trstd-switch tsId="X1234567890123456789012345678901"></trstd-switch>
<script type="module" src="https://widgets.trustedshops.com/switch/switch.js"></script>
For more detailed examples, please refer to the examples/
directory.
To get started with development, clone the repository and install the dependencies:
npm i
Setup the environment variables:
cp .env.example .env.local
and fill in the required values.
Generate local ssl certificates:
npm run generate-ssl-cert
To run local development server, use the following command:
npm run dev
To run the tests, use the following command:
npm test
To build the library, use the following command:
npm run build
Contributions are welcome! Please refer to the CONTRIBUTING.md file for guidelines.
This library is licensed under the MIT License - see the LICENSE file for details.
This project relies on several open-source libraries that have made development easier and more efficient. We would like to acknowledge the following libraries:
- jose - A comprehensive and robust JavaScript library for JSON Web Tokens (JWT) and related standards.
- node-cache - A simple and efficient in-memory caching module for Node.js.
- winston - A versatile and powerful logging library for Node.js.
We are grateful to the developers and maintainers of these projects for their hard work and dedication.
This project adheres to Semantic Versioning. For the versions available, see the tags on this repository.