Skip to content

Setup

Install Eicrud's client in your front-end to query your services.

 npm i @eicrud/client
Each CrudClient is configured for one CrudService.
const { CrudClient, ClientConfig } = require('@eicrud/client')

const config: ClientConfig = {
  serviceName: 'profile',
  url: "https://<eicrud_app_host>"
}
const profileClient = new CrudClient(config)
export interface ClientConfig { 
  serviceName: string, 
  url: string,
  onLogout?: () => void,
  storage?: ClientStorage, 
  id_field?: string,
  globalMockRole: string,
  defaultBatchSize?: number,
  cmdDefaultBatchMap?: { [key: string]: { batchField, batchSize: number} },
  limitingFields: string[],
  defaultProgressCallBack?: 
    (progress: number, total: number, type: 'limit' | 'batch') => Promise<void>,
};

Login

You can use the client as a guest or log in as a specific user.

const dto = {
  email: "myuser@mail.com",
  password: "p4ssw0rd",
  expiresInSec: 60*30
}

await profileClient.login(dto);

Note

Make sure to allow the login and check_jwt commands in your user service security. See this example.

expiresInSec

Indicates how long until the authentication token expires. Maximum expiresInSec is specified in the maxJwtexpiresInSec authentication option.

Info

If one of the clients encounters a 401 error, it will delete the JWT from storage, call the onLogout callback and retry the request as a guest.

checkJwt

You can call checkJwt to check if a user is currently logged in. It will extend the authentication duration if the renewJwt option is set.

const loggedUserId = await profileClient.checkJwt();

logout

It is possible to manually log out.

await profileClient.logout();