npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rb-auth-provider-simple

v0.23.0

Published

A Restboard auth provider supporting email/password or bearer token

Downloads

107

Readme

rb-auth-provider-simple

A Restboard auth provider supporting email/password or bearer token

Node.js CI

Getting started

import createAuthProvider from 'rb-auth-provider-simple'

const authProvider = createAuthProvider('https://my.api.url/auth')

authProvider.login({ email: '[email protected]', password: 'password' })
  .then(res => console.log(`Welcome, ${authProvider.getIdentity(res.data)}`))
  .catch(err => console.error(err))

Schema

Invoking login will call the provided authentication API as a POST request with the passed credentials (except for the keepLogged attribute which is used internally):

// e.g. login({ email = '...', password = '...', keepLogged = true })
//      will produce the following request payload:
{
  email: '...',
  password: '...'
}

This provder expects the authentication API will return a JSON response according to the following schema:

{
  user: {
    ...
  },
  token: '...'
}

Options

By default, a single string argument containing the URL of the authentication API to call on login can be passed to the factory function:

const authProvider = createAuthProvider('https://my.api.url/auth')

Additional options can be passed as second argument, e.g.:

const authProvider = createAuthProvider('https://my.api.url/auth', {
  tokenCacheKey: 'my-auth-token-cache-key'
})

| Name | Description | Default | |-----------------------|----------------------------------------------------------------|------------------| | recoverURL | The (optional) URL used to send a credentials recovery request | null | | activateOrResetURL | The (optional) URL used to activate or reset user credentials | null | | checkURL | The (optional) URL used to check for active authentication | null | | parseUserDetails | A function used to extract user details from the API response. Should have the following signature: (res) => object | (res) => res.user | | parseError | A function used to extract details from an API error response. Should have the following signature: (res) => object\|string | (res) => res?.statusText \|\| res?.status | | parseToken | A function used to extract the access token from the API response. Should have the following signature: (res) => string | (res) => res.token | | tokenCacheKey | The key used to store the bearer token into the cache storage | rb-auth-token | | userIdentifier | A function returning the user string representation | null | | tenantIdentifier | A function returning the user's tenant string representation | null | | acl | A function to check if user is allowed to perform action on subject. Should have the following signature: (user, action, subject) => boolean | null | | timeout | The timeout (ms) for each single HTTP request attempt | 5000 | | retries | The number of attempts before failing | 3 | | backoff | The incremental delay (ms) between request attempts | 300 | | client | The HTTP client used to perform the requests | cross-fetch | | writeToStorage | A function used to store a session value. Should have the following signature: async (key, val, persistent) => void | using local/sessionStorage | | readFromStorage | A function used to read a session value. Should have the following signature: async (key) => { value, persistent } | using local/sessionStorage | | removeFromStorage | A function used to remove a session value. Should have the following signature: async (key) => void | using local/sessionStorage |

CORS issues

If you encounter any CORS issue when using the provider, please keep in mind the default HTTP client is configured to include credentials for both same-origin and cross-origin requests.

If the server is configured to allow any origin (Access-Control-Allow-Origin: *), a CORS error will be thrown.

You can solve this issue overriding the default HTTP client adjusting its configuration, e.g.:

const myClient(url, opts) {
  return fetch(url, {
    ...opts,
    headers: {
      Accept: "application/json",
      ...opts.headers,
    },
  });
}

const authProvider = createAuthProvider('https://my.api.url/auth', {
  client: myClient
})

Test

npm test

Contribute

If you want, you can also freely donate to fund the project development:

Donate

Have you found a bug?

Please open a new issue on:

https://github.com/restboard/rb-auth-provider-simple/issues

License

Copyright (c) Emanuele Bertoldi

MIT License