Auth Entity Storage Rest Client Examples
Use these snippets to integrate sign-in and user administration flows from browser or server-side TypeScript applications.
EntityStorageAuthenticationAdminRestClient
import { EntityStorageAuthenticationAdminRestClient } from '@twin.org/api-auth-entity-storage-rest-client';
const adminClient = new EntityStorageAuthenticationAdminRestClient({
endpoint: 'https://api.example.org',
pathPrefix: 'v1'
});
console.log(adminClient.className()); // EntityStorageAuthenticationAdminRestClient
await adminClient.create({
email: '[email protected]',
password: 'StrongPassword123',
userIdentity: 'did:example:ops',
organizationIdentity: 'did:example:core',
scope: ['admin']
});
await adminClient.update({
email: '[email protected]',
userIdentity: 'did:example:ops:team',
scope: ['admin', 'support']
});
await adminClient.updatePassword('[email protected]', 'StrongPassword124', 'StrongPassword123');
const adminUser = await adminClient.get('[email protected]');
console.log(adminUser.email); // [email protected]
import { EntityStorageAuthenticationAdminRestClient } from '@twin.org/api-auth-entity-storage-rest-client';
const adminClient = new EntityStorageAuthenticationAdminRestClient({
endpoint: 'https://api.example.org',
pathPrefix: 'v1'
});
const byEmail = await adminClient.get('[email protected]');
const byIdentity = await adminClient.getByIdentity('[email protected]');
await adminClient.remove('[email protected]');
console.log(byEmail.email); // [email protected]
console.log(byIdentity.userIdentity); // did:example:ops:team
EntityStorageAuthenticationRestClient
import { EntityStorageAuthenticationRestClient } from '@twin.org/api-auth-entity-storage-rest-client';
const authClient = new EntityStorageAuthenticationRestClient({
endpoint: 'https://api.example.org',
pathPrefix: 'v1',
cookieName: 'access_token'
});
console.log(authClient.className()); // EntityStorageAuthenticationRestClient
const loginResponse = await authClient.login('[email protected]', 'correct-horse-battery-staple');
await authClient.updatePassword('correct-horse-battery-staple', 'correct-horse-battery-staple-2');
const refreshResponse = await authClient.refresh(loginResponse.token);
await authClient.logout(refreshResponse.token);
console.log(refreshResponse.expiry > 0); // true