Skip to content
/ client Public

Javascript client library for jouw.id

Notifications You must be signed in to change notification settings

jouw-id/client

Repository files navigation

jouw-id/client: authentication client for https://jouw.id/

import * as jouwid from "@jouw-id/client";

async function main() {
  await jouwid.login({
    client_id: "myClient",
    client_secret: "myClientSecret",
    keepLoggedIn: true,
    silent: true,
  });
  if (jouwid.isLoggedIn()) {
    let profile = await jouwid.getProtectedResource("/path/to/file");
  }
}
main();

Installation

npm install @jouw-id/client

You can also use a CDN (Content Delivery Network) like jsdelivr:

<script src="https://cdn.jsdelivr.net/npm/@jouw-id/[email protected]/dist/bundle.js"></script>

Use for example https://www.srihash.org/ to calculate the resource integrity for added security.

Usage

In ES6 modules:

import * as jouwid from "@jouw-id/client";

await jouwid.login({
  client_id: "myClient",
  client_secret: "myClientSecret",
  keepLoggedIn: true,
  silent: true,
});

Alternatively, when using a CDN:

<script src="https://cdn.jsdelivr.net/npm/@jouw-id/[email protected]/dist/bundle.js"></script>
<script>
  jouwid
    .login({
      client_id: "myClient",
      client_secret: "myClientSecret",
      keepLoggedIn: true,
      silent: true,
    })
    .then(() => {
      // ... do your thing
    });
</script>

API

login()

This triggers the login flow, using the Solid-OIDC (OpenID Connect) protocol. You must pass an options object as the first parameter, with the following settings:

  • client_id: Required. A string containing the client id for OIDC.
  • client_secret: Required. A string with the client secret for OIDC.
  • keepLoggedIn: Optional. Boolean. If true, the user will stay logged in between sessions.
  • silent: Optional. Boolean. Defaults to false. If true, the user won't be redirected to a login screen if the user is not logged in.
  • idpRedirect: Optional. Function: (url) => {}. If set, this function will be called when the user must log in to jouw.id, with the url to do so. The idpRedirect function can then handle this as required, e.g. open a popup or iframe to log in. On resolve, the user should be logged in.

isLoggedIn()

This function returns true if the user is logged in. It takes an options object as first parameter. The only option you can specify is:

  • keepLoggedIn: Optional. Boolean. If true, the login status is checked against localStorage, otherwise against sessionStorage. Keep this the same as in the login() call.

logout()

This function will logout the user in the browser and from the jouw.id provider as well. It takes an options object as first parameter. Available options are:

  • redirectURL: Optional. A url that is used to redirect the user after logging out.

getProtectedResource()

Will fetch a file (resource) from the master solid pod of the logged in user. It takes an options object as first parameter. Available options are:

  • resourcePath: Required. A valid path in the users master pod.

This will return either a javascript object, if the resource is a json or json-ld file. A text string if the resource is a text file, e.g. text/turtle. Or a Blob otherwise. If the resource is not found, it will return false.

const profileTurtle = await jouwid.getProtectedResource(
  "sndk/basicProfile.ttl",
);