Function: useCredentials()

typescript
1
useCredentials(): object

Defined in: hooks/index.ts:568

Hook for accessing credential storage context and operations.

Provides functionality for managing stored verifiable credentials including retrieval, deletion, and refresh operations. This hook must be used within a CredentialsProvider.

Returns

Credentials context containing:

  • credentials: Array of all stored verifiable credentials
  • deleteCredential: Async function to permanently delete a credential
  • fetchCredentials: Async function to refresh credentials from storage

credentials

typescript
1
credentials: Credential[]

Array of stored credentials

deleteCredential()

typescript
1
deleteCredential: (credential) => Promise<void>

Function to delete a credential

Parameters

credential

Credential

Returns

Promise<void>

fetchCredentials()

typescript
1
fetchCredentials: () => Promise<Credential[]>

Function to refresh credentials from storage

Returns

Promise<Credential[]>

Throws

When used outside of CredentialsProvider

Example

tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { useCredentials } from '@trust0/identus-react/hooks';
function CredentialWallet() {
const { credentials, deleteCredential, fetchCredentials } = useCredentials();
const handleDeleteCredential = async (credential) => {
if (window.confirm('Are you sure you want to delete this credential?')) {
try {
await deleteCredential(credential);
console.log('Credential deleted successfully');
} catch (error) {
console.error('Failed to delete credential:', error);
}
}
};
const refreshCredentials = async () => {
try {
await fetchCredentials();
console.log('Credentials refreshed');
} catch (error) {
console.error('Failed to refresh credentials:', error);
}
};
return (
<div>
<h3>My Credentials ({credentials.length})</h3>
<button onClick={refreshCredentials}>Refresh</button>
{credentials.map((credential, index) => (
<div key={index} className="credential-card">
<h4>{credential.claims.name || 'Unnamed Credential'}</h4>
<p>Issuer: {credential.issuer}</p>
<p>Type: {credential.credentialType}</p>
<p>ID: {credential.id}</p>
<button
onClick={() => handleDeleteCredential(credential)}
className="delete-button"
>
Delete
</button>
</div>
))}
{credentials.length === 0 && (
<p>No credentials stored. Accept a credential offer to get started.</p>
)}
</div>
);
}