2.0.20 • Published 2 months ago

js-oauth2 v2.0.20

Weekly downloads
10
License
MIT
Repository
github
Last release
2 months ago

js-oauth2

npm version License npm coverage report

This library is a port of angular-oauth2 to vanilla JS and fetch. Currently, this library only uses the password credential grant, i.e, using a combination (username, password), we'll request an access token (using grant_type=password) wich, in case of success, will return a response such as:

{
  "access_token": "foobar",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "foobiz"
}

Internally we'll automatically store it as a cookie and it will be used in every request adding an Authorization header: Authorization: 'Bearer foobar'.

Instalation

choose your preferred method:

  • npm: npm install --save js-oauth2
  • yarn: yarn add js-oauth2

Usage

initialize library:

import {
  AuthenticationService,
  AbstractKeychain,
  httpRequestInterceptor,
  type Token
} from 'js-oauth2'
import cookie from 'js-cookie'

class Keychain extends AbstractKeychain {
  async setToken (value: Token): Promise<void> {
    cookie.set('token', value)
  }

  async getToken (): Promise<Token> {
    return cookie.getJSON('token') || {}
  }

  async removeToken (): Promise<void> {
    cookie.remove('token')
  }
}

const oauth = new AuthenticationService({
  baseUrl: '/api',
  clientId: 'b921b25ebe30c6b1',
  clientSecret: '8f4d45dd922b2eb7',
  keychain: new Keychain()
})

httpRequestInterceptor(oauth) // only if you need http request interception

API

Check authentication status:

/**
 * Verifies if the `user` is authenticated or not based on the `token`
 * cookie.
 * @return {Promise<boolean>}
 */
oauth.isAuthenticated()

Get an access token:

/**
 * Retrieves the `access_token` and stores the `response.data` on cookies
 * using the `OAuthToken`.
 * @param {object} user - Object with `username` and `password` properties.
 * @param {object} config - Optional configuration object sent to `POST`.
 * @return {Promise} A response promise.
 */

oauth.getAccessToken(user, options)

Refresh access token:

/**
 * Retrieves the `refresh_token` and stores the `response.data` on cookies
 * using the `OAuthToken`.
 * @return {Promise} A response promise.
 */

oauth.getRefreshToken()

Revoke access token:

/**
 * Revokes the `token` and removes the stored `token` from cookies
 * using the `OAuthToken`.
 * @return {Promise} A response promise.
 */

oauth.revokeToken()

Catch OAuth errors and do something with them (optional):

const onError = async (response: Response) => {
  const data = await response.clone().json()

  if (data.error === 'invalid_grant') {
    return
  }
  // Refresh token when a `invalid_token` error occurs.
  if (data.error === 'invalid_token') {
    return oauth.getRefreshToken()
  }
  // Redirect to `/login` with the `error_reason`.
  return window.location(`/login?error_reason=${data.error}`)
}

auth.onError(onError)

NOTE: An event oauth:error will be sent everytime a onError is emitted:

  • { status: 400, data: { error: 'invalid_request' } }
  • { status: 400, data: { error: 'invalid_grant' } }
  • { status: 401, data: { error: 'invalid_token' } }
  • { status: 401, headers: { 'www-authenticate': 'Bearer realm="example"' } }

References to create project

3.0.0-beta.1

2 months ago

3.0.0-beta.3

2 months ago

3.0.0-beta.2

2 months ago

3.0.0-beta.4

2 months ago

2.0.20

3 years ago

2.0.19

3 years ago

2.0.18

5 years ago

2.0.17

6 years ago

2.0.16

6 years ago

2.0.15

6 years ago

2.0.14

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.0-0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

7 years ago

0.0.21

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago