@drewkimberly/udagram-auth
v1.0.0
Published
Authentication package for the Udagram app.
Readme
Udagram Auth
An authentication lib for the Udagram app.
This packaged is maintained via the Udagram Monorepo
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!');
});