@cognite/dune-fe-auth
v1.0.0
Published
Authentication utilities for Cognite Data Fusion React applications
Readme
@cognite/dune-fe-auth
Authentication utilities for Cognite Data Fusion React applications.
Installation
npm install @cognite/dune-fe-authUsage
CDFAuthenticationProvider
Wrap your application with the CDFAuthenticationProvider to provide CDF SDK context:
import { CDFAuthenticationProvider } from '@cognite/dune-fe-auth';
function App() {
return (
<CDFAuthenticationProvider>
<YourAppComponents />
</CDFAuthenticationProvider>
);
}useCDF Hook
Use the useCDF hook to access the Cognite SDK in your components:
import { useCDF } from '@cognite/dune-fe-auth';
function MyComponent() {
const { sdk } = useCDF();
// Use the SDK to interact with CDF
const assets = await sdk.assets.list();
return <div>...</div>;
}Utilities
The package also exports utility functions for authentication:
import { getToken, getLocalHostSDK, EMPTY_SDK } from '@cognite/dune-fe-auth';
// Get an access token
const token = await getToken(clientId, clientSecret);
// Get a configured SDK for localhost development
const sdk = await getLocalHostSDK();Environment Variables
For localhost development, ensure these environment variables are set:
PROJECT- Your CDF project nameBASE_URL- CDF base URLCLIENT_ID- OAuth client IDCLIENT_SECRET- OAuth client secret
Setup
Copy the provided template file:
cp .env.template .envFill in your actual values in the
.envfile
Peer Dependencies
This package requires:
@cognite/sdk^8.0.0react^18.0.0
Development
# Install dependencies
npm install
# Build the package
npm run build
# Watch for changes during development
npm run dev
# Preview the built package
npm run preview
# Type check
npm run type-checkPublishing
npm publishLicense
MIT
