@sensenet/authentication-jwt

JWT Authentication service for sensenet

Usage no npm install needed!

<script type="module">
  import sensenetAuthenticationJwt from 'https://cdn.skypack.dev/@sensenet/authentication-jwt';
</script>

README

@sensenet/authentication-jwt

This NPM package contains a client-side JWT authentication service implementation for sensenet.

NPM version NPM downloads License: GPL v2

Install

# Yarn
yarn add @sensenet/authentication-jwt

# NPM
npm install @sensenet/authentication-jwt

Setup and usage

You can use JWT authentication with a preconfigured sensenet >7.0.0 backend.

  • sessionLifetime - You can change how user sessions should be persisted on the client, you can use 'session', which means the user will be logged out when the browser is closed, or 'expiration', in that case the token expiration property will be used. This behavior is implemented for JWT Authentication.

Service setup:

import { TokenPersist } from '@sensenet/authentication-jwt'

const repository = new Repository()
const jwtService = new JwtService(repository, { select: 'all' }, 5000, TokenPersist.Expiration)

Login / logout:

You can log in and out using the following API endpoints:

const loginSuccess = await repository.authentication.login('username', 'password')
const logoutSuccess = await repository.authentication.logout()

State and user changes

You can subscribe to authentication state and current user changes using the following two observable values:

jwtService.currentUser.subscribe((newUser) => {
  console.log('User changed. New user: ', newUser.LoginName)
})

jwtService.state.subscribe((newState) => {
  console.log('Authentication state changed to', newState)
})

Authenticated requests

Please note that if you want to send custom authenticated requests to the content repository, always use the repository.fetch(...) method. This ensures that your access token will be renewed if needed and your authentication state will consistent.