2.0.3 • Published 5 years ago

express-jwt-util v2.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

express-auth-jwt 🚓

a node module

Getting Started

You will need Node >= 7 installed. How do I install node? click here to find out about nvm

Installation

# install module
npm install express-auth-jwt

# install deps
npm install jsonwebtoken body-parser cookie-parser cookie-session

Contributors

Example

const expressAuthJWT = require("express-auth-jwt");

// Dependencies
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const session = require("cookie-session");

// Setup
app.use(bodyParser.json());
app.use(cookieParser());
app.use(
  session({
    resave: false,
    secret: "keyboard cat",
    saveUninitialized: true,
    cookie: {
      httpOnly: true,
      maxAge: 3600
    }
  })
);
app.use((req, res, next) => {
  req.jsonResponse = req.headers.accept === "application/json";
  next();
});
const config = {
  database: {
    getUser: Promise.resolve({
      _id: "95fec9bf-5baa-4ccf-aa3b-c0cfea46bdff",
      username: "admin",
      password: "admin",
      name: "admin"
    })
  }
};

// Using the module
const { authEndpoints, authenticate } = expressAuthJWT(config);

app.use(authEndpoints);

app.get("/secret/route", authenticate, (req, res) =>
  res.json("this is private")
);

Default Config

const defaultConfig = {
  database: {
    getUser: () =>
      Promise.reject(
        new Error(
          "Function getUser, not specified in the express-auth-jwt(config)"
        )
      ),
    findQuery: { username: true },
    userIdField: "_id",
    comparePasswords: (dbPassword, requestPassword) =>
      dbPassword === requestPassword
  },
  jwt: {
    secret: "sekret"
  },
  session: {
    cookiePropId: "_id",
    // ---------------- hr | min | sec | mili
    timeToExpireSession: 1 * 60 * 60 * 1000
  },
  messages: {
    tokenNotFound: "No token provided.",
    tokenExpired: "The session has expired.",
    failToAuthenticate: "Could not authenticate.",
    userNotFound: "The user is not valid.",
    wrongPassword: "The password is incorrect.",
    loginSucess: "Enjoy your token!",
    logout: "logout"
  }
};
2.0.3

5 years ago

2.0.2

5 years ago

2.0.0

5 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago