@fullerstack/ngx-jwt
A simple JWT library for Angular applications
Overview
Description
This library helps with verification and payload extraction of JWT tokens
@fullerstack/ngx-jwt attempts to streamline the jwt operation of your Angular application, while promoting DRY DRY.
How to install
npm i @fullerstack/ngx-jwt |OR| yarn add @fullerstack/ngx-jwt
How to use
import { ApplicationConfig } from '@fullerstack/ngx-config';
import { LogLevel } from '@fullerstack/ngx-logger';
export const environment: ApplicationConfig = {
appName: '@fullerstack/ngx-jwt',
production: false,
log: {
level: LogLevel.debug,
},
jwt: {
networkDelay: 1,
expiryLeeway: 5,
},
};
import { ConfigModule } from '@fullerstack/ngx-config';
import { LoggerModule } from '@fullerstack/ngx-logger';
import { JwtModule } from '@fullerstack/ngx-jwt';
import { environment } from '../environments/environment';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
ConfigModule.forRoot(environment),
LoggerModule,
JwtModule,
],
bootstrap: [AppComponent],
})
export class AppModule {}
import { Component } from '@angular/core';
import { ConfigService } from '@fullerstack/ngx-config';
import { LoggerService } from '@fullerstack/ngx-logger';
import { JwtService } from '@fullerstack/ngx-jwt';
@Component({
selector: 'fullerstack-root',
template: `<h1>Welcome to {{ title }}!</h1>`,
})
export class AppComponent {
title = 'Fullerstack';
options = {};
constructor(
readonly config: ConfigService,
readonly logger: LoggerService,
readonly jwt: JwtService
) {
this.title = this.config.options.appName;
this.logger.info('AppComponent loaded ...');
const someToken = 'some-jwt-token-received-from-server';
const payload = this.jwt.getPayload(someToken);
const isExpired = this.jwt.isExpired(payload);
if (!isExpired) {
const userId = payload.sub;
const nextRefresh = this.jwt.getRefreshTime(payload);
setTimeout(() => {
}, nextRefresh * 1000);
}
}
}
License
Released under a (MIT) license.
Version
X.Y.Z Version
`MAJOR` version -- making incompatible API changes
`MINOR` version -- adding functionality in a backwards-compatible manner
`PATCH` version -- making backwards-compatible bug fixes