2max-express-authenticate 中文文档教程

发布于 7年前 浏览 25 项目主页 更新于 3年前

Express authenticate

基于Sequelize的express简单认证。

Install

npm i 2max-express-authenticate --save

Configuration

const express = require('express');
const app = express();
const auth = require('2max-express-authenticate');

async function main(app) {
  // Declare auth + configure for persistence
  const util = await auth(app, {
    'persistence': {
      'host': process.env.MYSQL_HOST,
      'user': process.env.MYSQL_USER,
      'password': process.env.MYSQL_PASSWORD,
      'database': process.env.MYSQL_DATABASE,
      'dialect': 'mysql'
    }
  });

  // Secure yours routes with auth
  util.secure([
    '/admin',
    '/dashboard',
    ...
  ]);

  // Override connection method for send custom results
  util.override({
    'login': (req, res) => {
      res.send('Congrats');
     },
     'logout': (req, res) => {
       res.send('OUT');
     },
     'signup': (req, res) => {
       res.send('Good job mother fucker men');
     }
  });
}

app.listen(80);

Route

MethodendpointBody
POST/signup{username, password}
POST/login{username, password}
GET/logoutN/A

Features

  • Crypt password with bcrypt
  • Login, logout and signup (cookies + session)
  • Persist data (according to sequelize compatibility)
  • Give array with route to check session for each route
  • Enable middlewares for set response from /login, /logout and /signup

Todo

  • Add roles checking (bitmask)
  • Secure route according to roles
  • User can add role

Express authenticate

Simply authentication for express based on Sequelize.

Install

npm i 2max-express-authenticate --save

Configuration

const express = require('express');
const app = express();
const auth = require('2max-express-authenticate');

async function main(app) {
  // Declare auth + configure for persistence
  const util = await auth(app, {
    'persistence': {
      'host': process.env.MYSQL_HOST,
      'user': process.env.MYSQL_USER,
      'password': process.env.MYSQL_PASSWORD,
      'database': process.env.MYSQL_DATABASE,
      'dialect': 'mysql'
    }
  });

  // Secure yours routes with auth
  util.secure([
    '/admin',
    '/dashboard',
    ...
  ]);

  // Override connection method for send custom results
  util.override({
    'login': (req, res) => {
      res.send('Congrats');
     },
     'logout': (req, res) => {
       res.send('OUT');
     },
     'signup': (req, res) => {
       res.send('Good job mother fucker men');
     }
  });
}

app.listen(80);

Route

MethodendpointBody
POST/signup{username, password}
POST/login{username, password}
GET/logoutN/A

Features

  • Crypt password with bcrypt
  • Login, logout and signup (cookies + session)
  • Persist data (according to sequelize compatibility)
  • Give array with route to check session for each route
  • Enable middlewares for set response from /login, /logout and /signup

Todo

  • Add roles checking (bitmask)
  • Secure route according to roles
  • User can add role
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文