1.0.0 • Published 4 years ago

@drewkimberly/udagram-auth v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

Udagram Auth

An authentication lib for the Udagram app.

This packaged is maintained via the Udagram Monorepo

npm version

Usage

UdagramJWT

This module exports the class UdagramJWT<P extends UdagramJWTPayload> which is a small abstraction around JWT. The available API includes:

generateToken(payload: P): string

Generates a signed JWT token given a payload.

verifyToken(token: string, callback?: VerifyCallback): void

Verifies the provided JWT token. Verification logic is provided by the callback parameter, which is documented in the JWT library here.

requireAuth

This module exports an ExpressJS middleware handler:

const requireAuth = (
  jwt: UdagramJWT<UdagramJWTPayload>
): RequestHandler => (req: Request, res: Response, next: NextFunction);

This middleware will verify that incoming API requests have an Authorization header and that the provided token satisfies the given JWT. For example:

import express from 'express';
import {requireAuth, UdagramJWT} from '@drewkimberly/udagram-auth'

const app = express();
app.get('/', requireAuth(new UdagramJWT('my-secret')), async (req, res) => {
  res.send('JWT Token Verified!');
});