npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webacad/ng-jwt-auth

v1.1.0

Published

JWT authentication for angular

Downloads

21

Readme

NPM version Build Status

WebACAD/NgJwtAuth

JWT authentication for angular.

Includes

  • Authorization service
  • HTTP interceptor
  • Basic logged in router guard
  • Auto login on page refresh (uses store to store the token in browser)

Dependencies

  • @angular/core
  • @angular/common
  • @angular/router
  • @webacad/ng-store
  • jwt-decode
  • rxjs@^5.5.0

Installation

Install with npm:

npm install --save @webacad/ng-jwt-auth

or with yarn:

yarn add @webacad/ng-jwt-auth

Configuration

First create a new class which will extend the AbstractAuthConfigurator. This new class will be used as a bridge between your application and this library.

import {Injectable} from '@angular/core';
import {HttpResponse, HttpErrorResponse} from '@angular/common/http';
import {AbstractAuthConfigurator} from '@webacad/ng-jwt-auth';
import {Observable} from 'rxjs/Observable';

import {UsersRepository, User} from '../model/users';

@Injectable()
export class AuthConfigurator extends AbstractAuthConfigurator<User>
{

    constructor(
        private $users: UsersRepository,
    ) {
        super({
            withCredentials: true,
            loginPage: '/login',
            tokenStorage: 'sm_jwt_data',
        });
    }

    public isServerLogout(err: HttpErrorResponse): boolean
    {
        return err.status === 403;
    }

    public extractToken(response: HttpResponse<any>): string|undefined
    {
        return response.body.auth.token;
    }

    public getUserByToken(token: any): Observable<User>
    {
        return this.$users.get(token.jti);
    }

    public login(data: any): Observable<User>
    {
        return this.$users.login(data.email, data.password);
    }

}

Your application must have some user entity.

Options:

  • withCredentials: (boolean, default: false), option passed to @angular/common/http interceptor: api
  • loginPage: (string, default: /login), router link to your login page
  • tokenStorage: (string, default: ng-jwt-auth-token-data), key which will be used for storing your jwt token in browser storage

Methods:

  • isServerLogout:
    • Method called from @angular/common/http interceptor
    • Called on error response
    • Should return true if server wants you to logout the user
  • extractToken:
    • Method called from @angular/common/http interceptor
    • Should return raw string token from http response or undefined if token does not exists in response
  • getUserByToken:
    • Method called on page refresh if jwt token exists in browser storage (auto login on refresh)
    • Receives decoded jwt token from browser storage
    • Must return the Observable object with your user
  • login:
    • Method called when user is being signed into your application
    • Must return the Observable object with your user
  • logout (not required):
    • Method called when used is being logged out from your application

Register configurator and ng-jwt-auth module

Now you only have to register your configurator class as a service and import the ng-jwt-auth module:

import {NgModule} from '@angular/core';
import {AuthModule, AbstractAuthConfigurator} from '@webacad/ng-jwt-auth';
import {AuthConfigurator} from './auth';

@NgModule({
    imports: [
        AuthModule.forRoot(),
    ],
    providers: [
        {
            provide: AbstractAuthConfigurator,
            useClass: AuthConfigurator,
        },
    ],
})
export class AppModule {}

AuthService

AuthService can be used for user authentication. It contains all the necessary methods and events:

  • event onLogin: Called after user was logged in
  • event onLogout: Called before user is logged out
  • getter loggedIn: Returns Observable<true> if user is currently signed in. Observable<false> otherwise.
  • getter user: Returns Observable<User> if user is currently signed in. Observable<undefined> otherwise.
  • method login(data: any): Should be used for user login.
  • method logout(): Should be used for user logout.

Example of usage:

import {AuthService} from '@webacad/ng-jwt-auth';
import {Observable} from 'rxjs/Observable';
import {User} from '../model/users';

export class UserInfo
{
    
    constructor(
        private $auth: AuthService<User>,
    ) {
        this.$auth.onLogin.subscribe(() => {
            alert('User was logged in');
        });
        
        this.$auth.onLogout.subscribe(() => {
            alert('User was logged out');
        });
    }
    
    public isLoggedIn(): Observable<boolean>
    {
        return this.$auth.loggedIn;
    }
    
    public getUser(): Observable<User|undefined>
    {
        return this.$auth.user;
    }
    
    public login(email: string, password: string): Observable<User>
    {
        return this.$auth.login({
            email,
            password,
        });
    }
    
    public logout(): void
    {
        this.$auth.logout();
    }
    
}

HTTP interceptor

The build in AuthHttpInterceptor is automatically registered into your application.

It automatically:

  • Adds the bearer authorization token into all requests if user is logged in
  • Monitors inactivity logouts from server
  • Automatically updates the jwt token after each request (if the token is present in HTTP response)

LoggedInAuthGuard

Simple router guard which prohibits access to route(s) for all anonymous users.

See angular documentation for how to use the guard.