0.8.0 • Published 1 year ago

@isic/client v0.8.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
1 year ago

ISIC Client

npm (scoped)

A client library for authenticating with the ISIC Archive from an SPA (single page application).

Usage

  • Install the library:

    yarn add @isic/client

    or if you're using npm:

    npm install @isic/client
  • Instantiate an IsicClient with your application-specific configuration:

    import IsicClient from '@isic/client';
    
    const isicClient = new IsicClient(
      process.env.CLIENT_ID, // e.g. 'v1odYySCetBht6DT9svQdAkvmVXrRHOwIIGNk6JG'
    );

    or if connecting to the sandbox server:

    const isicClient = new IsicClient(
      process.env.CLIENT_ID,
      {
        isicOrigin: new URL('https://api-sandbox.isic-archive.com'),
      },
    );
  • Call redirectToLogin when it's time to start a login flow:

    document.querySelector('#sign-in-link').addEventListener('click', (event) => {
      event.preventDefault();
      isicClient.redirectToLogin();
      // This will redirect away from the current page
    });
  • At the start of every page load, unconditionally call maybeRestoreLogin, to attempt to restore a login state; this will no-op if no login is present. Afterwards, get and store HTTP headers for new API accesses from authHeaders.

    let authHeaders;
    
    isicClient.maybeRestoreLogin()
      .then(() => {
        authHeaders = isicClient.authHeaders;
      })

    or, if using ES6 and async/await:

    await isicClient.maybeRestoreLogin();
    let { authHeaders } = isicClient;
  • Use these credentials for Ajax API requests:

    fetch('https://api.isic-archive.com/api/v2/studies/', {
      headers: authHeaders,
    });
  • The login state will persist across page refreshes. Call logout to clear any active login:

    document.querySelector('#sign-out-link').addEventListener('click', (event) => {
      event.preventDefault();
      isicClient.logout()
        .then(() => {
          authHeaders = isicClient.authHeaders;
        });
    });
0.8.0

1 year ago

0.7.0

2 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.3.0

3 years ago

0.4.0

3 years ago

0.2.0

4 years ago

0.1.0

4 years ago