Skip to content

Repository files navigation

@distributed/amon

Code Stylenpm version
npm downloadsPackage PhobiaBundle Phobia

Small lib exposing reusable utility methods for JWT authentication, using @node-rs/bcrypt & jsonwebtoken under the hood.

Install

With yarn:

yarn add @distributed/amon

With npm:

npm install @distributed/amon

Usage

Auth

Requirements:

Use process.env.APP_SECRET or pass the appSecret into createAuthPayload / createJwtToken functions.

You can execute the following command to generate your secret:

node -e "console.log(crypto.randomBytes(32).toString('hex'))"
# .env
APP_SECRET="45e05712755026248ef0f8f9881182b2cc3db28e64fcc42fb19d3209f5f0d19c"

Create password hash

import{createPasswordHash}from'@distributed/amon';consthashPassword=awaitcreatePasswordHash('foo');// => $2a$10$2M95zVobIQOm9BgNmKh/gu7IkH/LM45ZqsySlUQaFLrqAhppvm5Ei

Password validation

import{isPasswordValid}from'@distributed/amon';consthashPassword=awaitcreatePasswordHash('bar');constvalid=awaitisPasswordValid('bar',hashPassword);// => true

Get userId

import{getUserId}from'@distributed/amon';importfastifyfrom'fastify';constapp=fastify();app.get('/',async(request,reply)=>{constuserId=getUserId(request.headers);reply.type('application/json').code(200);return{ userId };});

Create Authpayload

import{getUserId}from'@distributed/amon';importfastifyfrom'fastify';constapp=fastify();app.get('/',async(request,reply)=>{typeUser={username: 'batman'};constuserId=getUserId(request.headers);constuser=awaitdb.findUnique({where: {id: userId}});constauthPayload=awaitcreateAuthPayload<User>(userId,user);reply.type('application/json').code(200);returnauthPayload;});

Create Jwt Token

import{createJwtToken}from'@distributed/amon';constuserId='foo';// JWT Signing optionsconstoptions={};// App Secret, if process.env.APP_SECREt is not setconstappSecret='bar';consttoken=awaitcreateJwtToken({ userId, options, appSecret });

Development

  1. Install dependencies using yarn install or npm install
  2. Start development server using yarn watch

About

Small library exposing reusable utility methods for JWT auth

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages