@ev-fns/auth
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@ev-fns/auth

Authorization middleware for expressjs

  • createAuth createAuth: ({ token, getToken }: { token: string, getToken?: (req: express.Request) => string }) => express.RequestHandler

version node downloads dependencies

Install

yarn add express @ev-fns/auth

Usage

const express = require("express");
const { createAuth } = require("@ev-fns/auth");

const app = express();

const auth = createAuth({ token: process.env.API_TOKEN });

app.get("/", auth, (req, res) => {
  res.status(200).json({ message: "Hello World 👋!" });
});

app.use((err, req, res, next) => {
  res.status(err.status || 500).json({ message: err.message });
});

app.listen(3000, () => {
  console.log("listening at http://localhost:3000");
});

Try it out

$ API_TOKEN=super_secret node index.js
  1. Invalid request

    $ curl -i http://localhost:3000
    HTTP/1.1 401
    ...
    {"message":"Unauthorized"}
  2. Valid request

    $ curl -i -H "Authorization: Bearer super_secret" http://localhost:3000
    HTTP/1.1 200
    ...
    {"message":"Hello World 👋!"}

Package Sidebar

Install

npm i @ev-fns/auth

Weekly Downloads

53

Version

1.0.1

License

MIT

Unpacked Size

3.22 kB

Total Files

4

Last publish

Collaborators

  • eliseuvideira