auth-oidc
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Authenticate users with OpenID Connect authentication service (f.e. Keycloak)

License

Main features:

  1. TypeScript support
  2. ESModule support
  3. Working with React, Angular and other popular front-end libraries
  4. Logging user in using redirect to the auth service
  5. Getting user info claims
  6. Supplying access token for requests to the back-end
  7. Automatic refreshing of the access token with refresh token when it expires
  8. Logging user out from the application and from the auth service

How to use

1. Install library using npm

npm install auth-oidc --save

2. Import AuthService

import {AuthService} from 'auth-oidc'

3. Initialize AuthService

new AuthService(config)

Config object fields:

  1. authority: URL to the authentication service (f.e. http://[host]/realms/[realm-name])
  2. clientId: ID of the application registered within authentication service
  3. (OPTIONAL) errorHandler: callback function that will be called in case of auth errors
    • Defaults to (error) => console.log(error)
  4. (OPTIONAL) autoLogin: Determines whether authentication should start automatically when page loaded
    • Defaults to false

4. Start login

Login will be started automatically if it was configured to do so, if no, you can start it by

authService.login()

You can also specify a URL the user will be returned to:

authService.login('http://loclahost:3000')

5. Check login status

You can check login status with

authService.isLoggedIn()

6. Get user info claims

To get user info claim you can use

authService.getUserInfo('name')

7. Get access token to make requests

You can get user access token with

authService.getToken()

8. Force refresh

You can force lib to refresh tokens and user info with:

authService.tryToRefresh()

9. Logout user

You can log out user from your application and authentication service with

authService.logout()

You can also specify a URL the user will be returned to:

authService.logout('http://loclahost:3000')

Copyright

Released under the MIT License. See the LICENSE file.

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i auth-oidc

    Weekly Downloads

    121

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    61 kB

    Total Files

    33

    Last publish

    Collaborators

    • misha999777