0.21.1 • Published 4 months ago

rb-auth-provider-simple v0.21.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

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: 'a@a.it', 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'
})
NameDescriptionDefault
checkURLThe (optional) URL used to check for active authenticationnull
parseUserDetailsA function used to extract user details from the API response. Should have the following signature: (res) => object(res) => res.user
parseTokenA function used to extract the access token from the API response. Should have the following signature: (res) => string(res) => res.token
tokenCacheKeyThe key used to store the bearer token into the cache storagerb-auth-token
userIdentifierA function returning the user string representationnull
tenantIdentifierA function returning the user's tenant string representationnull
aclA function to check if user is allowed to perform action on subject. Should have the following signature: (user, action, subject) => booleannull
timeoutThe timeout (ms) for each single HTTP request attempt5000
retriesThe number of attempts before failing3
backoffThe incremental delay (ms) between request attempts300
clientThe HTTP client used to perform the requestscross-fetch
writeToStorageA function used to store a session value. Should have the following signature: async (key, val, persistent) => voidusing local/sessionStorage
readFromStorageA function used to read a session value. Should have the following signature: async (key) => { value, persistent }using local/sessionStorage
removeFromStorageA function used to remove a session value. Should have the following signature: async (key) => voidusing 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

0.21.1

4 months ago

0.21.0

4 months ago

0.20.1

9 months ago

0.20.0

1 year ago

0.19.0

1 year ago

0.18.0

1 year ago

0.17.0

1 year ago

0.16.0

2 years ago

0.12.0

2 years ago

0.11.1

2 years ago

0.13.0

2 years ago

0.14.0

2 years ago

0.15.0

2 years ago

0.14.1

2 years ago

0.10.0

2 years ago

0.11.0

2 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.3.0

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.3.2

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.5.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago