# ng-yajwt

> Yet Another JWT Handler

Latest version **1.0.0** (published 2018-09-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install ng-yajwt
pnpm add ng-yajwt
yarn add ng-yajwt
bun add ng-yajwt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-09-12 |
| First published | 2018-09-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Adam Kunz |
| Maintainers | agkay |

## Links

- npm: https://www.npmjs.com/package/ng-yajwt
- Repository: https://github.com/agkunz/ng-yajwt
- Homepage: https://github.com/agkunz/ng-yajwt#readme
- Issues: https://github.com/agkunz/ng-yajwt/issues
- npm.io page: https://npm.io/package/ng-yajwt

## Recent versions

- 1.0.0 (latest) — 2018-09-12

## README

# Yet Another JWT Handler

## Introduction

Inspired by angular-jwt.

## Installation

Install with bower:
````bash
bower install ng-yajwt --save
````

## Configuration


At the very minimum, you should define what happens when your token is rejected.

````js
angular.module('app', ['ngYajwt'])
    .config(jwt);

jwt.$inject = ['$httpProvider', 'jwtInterceptProvider'];
function jwt ($httpProvider, jwtInterceptProvider)
{
    jwtInterceptProvider.unauthorized = ['Auth', 'logger', '$state', unauthorized];
    function unauthorized (Auth, logger, $state)
    {
        logger.error ('Your session has expired.');

        Auth.logout();
        
        return $state.go ('home');
    }
    
    jwtInterceptProvider.forbidden = ['logger', '$state', forbidden];
    function forbidden (Auth, logger, $state)
    {
        logger.error ('You\'re not allowed to do that!');

        return $state.go ('profile');
    }

    $httpProvider.interceptors.push('jwtIntercept');
}
````

If you'd like to automatically refresh an expired token, just tell us how to go about doing it.

````js
jwtInterceptProvider.useRefreshTokens = true;

jwtInterceptProvider.refreshToken = ['Auth', refreshToken];
function refreshToken (Auth)
{
    console.log ('Refreshing your login token...');

    // we're expecting you to return a promise here
    return Auth.refreshToken();
    // return $http.post ('/auth/refresh', { auth : false });
}
````

By default, if localStorage is available we'll store the token there.  If not, we'll store it in the cookie.
If you want to control how we store the token, just supply a getter and a setter.

````js
jwtInterceptProvider.getToken = ['Auth', getToken];
function getToken (Auth)
{
    var token = Auth.getToken();

    console.log ('using ' + token.slice(-10));
    
    return token;
}

// the token will be injected here for you to save wherever you want
jwtInterceptProvider.setToken = ['Auth', 'token', setToken];
function setToken (Auth, token)
{
    console.log ('setting ' + token.slice(-10));
    
    // i personally inject gsklee/ngStorage into my auth service and store it there
    return Auth.setToken(token);
}
````

## Usage

This package will watch all responses for an Authorization header, and assume that to be the next token we pass.

Some requests don't need valid authorization, such as your token refresh route.

````js
return $http.get('/public/route', { auth : false });
````

## Credits

I was using https://github.com/auth0/angular-jwt, but had slightly different requirements.  You are probably better off using that instead.

---
_Source: https://npm.io/package/ng-yajwt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
