Blog>
Snippets

Securing Sensitive Configuration Data

Example showing how to securely manage sensitive information (like API keys) with TanStack Config, including practices for encrypting and safely accessing secrets in an e-commerce setting.
import { getConfig, createConfig } from '@tanstack/config';

// Define sensitive keys
const keys = {
  stripeSecretKey: '',
  databaseUrl: ''
};

// Encrypt and store configuration
export const secureConfig = createConfig({
  key: 'encryptedConfig',
  default: keys,
  // You would replace this with real encryption logic
  encrypt: (config) => JSON.stringify(config),
  decrypt: (encryptedConfig) => JSON.parse(encryptedConfig)
});
This snippet demonstrates how to define, encrypt, and store sensitive configuration using TanStack Config. The `createConfig` function is used to encrypt and manage sensitive information. Encryption and decryption functions should be implemented based on your specific security requirements.
import { useConfig } from '@tanstack/config-react';

function useSecureConfig() {
  // Access encrypted configuration
  const encryptedConfig = useConfig().getConfig('encryptedConfig');
  // Decrypt the configuration to use in the application
  const decryptedConfig = encryptedConfig.decrypt(encryptedConfig.value);
  return decryptedConfig;
}
This code snippet shows how to access and decrypt the encrypted configuration using a custom hook `useSecureConfig`. The `useConfig` hook from '@tanstack/config-react' is utilized to retrieve the encrypted configuration, which is then decrypted for use within the application.