@a11d/api-jwt
v0.2.1
Published
An extension of @a11d/api for JWT bearer authentication, keeping the tokens in local storage and refreshing them on a 401.
Readme
@a11d/api-jwt
An extension of @a11d/api for JWT bearer authentication, keeping the tokens in local storage and refreshing them on a 401.
Installation
npm install @a11d/api-jwtUsage
import { Api, apiAuthenticator } from '@a11d/api'
import { JwtApiAuthenticator } from '@a11d/api-jwt'
@apiAuthenticator()
export class Authenticator extends JwtApiAuthenticator {
constructor() {
super({ refresh: refreshToken => Api.post<string>('/token/refresh', { refreshToken }) })
}
}
type Tokens = {
readonly token: string
readonly refreshToken: string
}
const tokens = await Api.post<Tokens>('/token', { username: 'user', password: 'secret' })
JwtApiAuthenticator.token = tokens.token
JwtApiAuthenticator.refreshToken = tokens.refreshTokenAPI
| Name | Kind | Description |
| --- | --- | --- |
| JwtApiAuthenticator | class | Authenticates the requests of Api with a JWT bearer token kept in local storage, refreshing it on a 401 when configured to. |
Links
License
MIT © a11delavar
