npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

gbv-login-client

0.2.6 • Public • Published

Login Client

Build Status GitHub package version NPM package name standard-readme compliant

This repository offers a JavaScript client to be used with login-server.

Table of Contents

Install

npm install gbv-login-client

To include login-client via a CDN, see below.

Build

git clone https://github.com/gbv/login-client.git
cd login-client
npm install
npm run build

browserify is used to create builds for the browser. These builds are available in the build/ folder and will be created on each commit. The files can be included manually or using a CDN like jsDelivr:

<script src="https://cdn.jsdelivr.net/gh/gbv/login-client/build/login-client.js"></script>

Or minified:

<script src="https://cdn.jsdelivr.net/gh/gbv/login-client/build/login-client.min.js"></script>

After that, the class LoginClient can be used like shown below.

Usage

// Not needed when the browser build was included.
const LoginClient = require("gbv-login-client")
// Second parameter is an options object with properties:
// `ssl` (default: true), `retryMs` (default: 1000), `retryMsMax` (default: 10000), `retryMult` (default: 1.2)
let client = new LoginClient("login.example.com")
// Add event listeners
// Note: `event` always contains the property `event.type` which is the name of the event.
client.addEventListener(LoginClient.events.connect, event => {
  // Fires when the client successfully connected.
  // `event` is empty.
})
client.addEventListener(LoginClient.events.disconnect, event => {
  // Fires when the client disconnected.
  // `event` is empty.
})
client.addEventListener(LoginClient.events.login, event => {
  // Fires when the user has logged in.
  // `event.user` contains the user object.
})
client.addEventListener(LoginClient.events.logout, event => {
  // Fires when the user has logged out.
  // `event` is empty.
})
client.addEventListener(LoginClient.events.update, event => {
  // Fires when the user was updated.
  // `event.user` contains the updated user object.
})
client.addEventListener(LoginClient.events.providers, event => {
  // Fires when the providers were updated.
  // `event.providers` contains the updated providers list.
})
client.addEventListener(LoginClient.events.about, event => {
  // Fires when the server's about information was updated.
  // `event` contains the information (e.g. `event.publicKey`).
})
client.addEventListener(LoginClient.events.token, event => {
  // Fires when the token was updated.
  // `event.token` contains the updated token,
  // `event.expiresIn` contains the number of seconds the token will expire in.
})
client.addEventListener(LoginClient.events.error, event => {
  // Fires when an error occurred.
  // `event.error` contains one of the following errors:
  // - LoginClient.errors.NoInternetConnectionError
  // - LoginClient.errors.ThirdPartyCookiesBlockedError
  // - LoginClient.errors.ServerConnectionError
})
// (normally not used in production)
client.addEventListener(LoginClient.events._sent, event => {
  // Fires when a message was sent through the WebSocket.
  // `event.message` contains the message that was sent.
})
// (normally not used in production)
client.addEventListener(LoginClient.events._received, event => {
  // Fires when a message was received through the WebSocket.
  // `event.message` contains the message that was received.
})
// Alternatively, you can set an event listener for `null` which receives all events:
client.addEventListener(null, event => {
  switch (event.type) {
    case LoginClient.events.connect:
      // ...
      break
    default:
      // ...
  }
})
// Connect
client.connect()
// Access properties
client.loggedIn
client.user
client.providers
client.connected
client.token
client.decodedToken // Decoded, but not verified!
client.about
// Additional methods
client.setName("New Name")
// Static properties
LoginClient.events // Object with available events (usage see above)
LoginClient.errors // Object with available error classes
LoginClient.jwtDecode // Access to jwtDecode function

The login-server contains a more comprehensive example at its /api endpoint. See its source code for details.

Test

npm test

Maintainers

Publish

To publish a new version on npm after committing your changes, follow these steps:

npm version patch # or minor, or major 
git push --tags origin master

Travis will automatically deploy the new version based on the tag to npm.

Contribute

PRs accepted.

  • Please run the tests before committing.
  • Please do not skip the pre-commit hook when committing your changes.
  • If editing the README, please conform to the standard-readme specification.

License

MIT © 2019 Verbundzentrale des GBV (VZG)

Keywords

none

install

npm i gbv-login-client

Downloadsweekly downloads

9

version

0.2.6

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability