npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auth-api

v1.0.10

Published

generate auth api for you

Downloads

20

Readme

Purpose

Reuse authentication part code of REST server, easily and flexibly. Thanks to express.Router.

Features

  • jwt to verify user;
  • nodemailer to send verification emails;
  • mongoose to drive mongodb (user model: https://github.com/timqian/auth-api/blob/master/src/models/User.js);
  • axios to test RESTful api(axios can be used both on browser and node, that means the test code can be reused in your web app);

Sample usage:

  1. Install auth-api and his peerDependencies:

npm install auth-api express body-parser mongoose --save

  1. Run the sample code below and boom~~ the auth server will be listening at http://localhost:3000
var authApi        = require('auth-api');
var express        = require('express');
var bodyParser     = require('body-parser');
var mongoose       = require('mongoose');

mongoose.connect('mongodb://localhost/database'); // connect to database

var userConfig = {
  APP_NAME: 'STOCK APP',
  SECRET: 'ilovetim',                             // jwt secret
  CLIENT_TOKEN_EXPIRES_IN: 60 * 24 * 60 * 60,     // client token expires time(60day)
  EMAIL_TOKEN_EXPIRES_IN: 24 * 60 * 60,           // email token expires time(24h)

  EMAIL_SENDER: {                                 // used to send mail by nodemailer
    service: 'Gmail',
    auth: {
      user: '[email protected]',
      pass: '321qianqian',
    }
  },

  USER_MESSAGE: {                                 // message sent to client
    MAIL_SENT: 'mail sent',
    NAME_TAKEN: 'Name or email has been taken',
    USER_NOT_FOUND: 'User not found',
    WRONG_PASSWORD: 'wrong password',
    LOGIN_SUCCESS: 'Enjoy your token!',
    NEED_EMAIL_VERIFICATION: 'You need to verify your email first',
  },

  API_URL: 'http://localhost:3000'              // to be used in the mail
};

authApi.init(userConfig);

var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', authApi.authRouter);

// protecting api
app.get('/needingToken', authApi.verifyToken, (req, res) => {

  // send back the jwt claim directly
  var claim = req.decoded;
  res.status(200).json(claim);
});

app.get('/needingTokenAndEmailVerified', authApi.verifyToken, (req, res) => {
  if (req.decoded.verified) {
    res.status(200).json(req.decoded);
  } else {
    res.status(400).json({
      success: false,
      message: 'Please verify your email before doing this!'
    });
  }
});


app.listen(3000);
console.log('API magic happens at http://localhost:3000');

// handle unhandled promise rejection
// https://nodejs.org/api/process.html#process_event_unhandledrejection
process.on('unhandledRejection', function(reason, p) {
    console.log('Unhandled Rejection at: Promise ', p, ' reason: ', reason);
    // application specific logging, throwing an error, or other logic here
});

(es6 sample: https://github.com/timqian/auth-api/blob/master/testServer.js)

What does the above code do for you

  1. Generate the following auth api for you at http://localhost:3000

|Method| url | data(if needed) | server action(if request is good) | | ---- |---------------------| ---------------------------------------------| -------------| | POST | /signup | {name: ..., email: ..., password: ...} |create a user in mongodb and send verification email | | POST | /login | {name/email: ..., password: ...} |check user and return jwt token| | POST | /password_reset | {email: ..., password(the new password): ...}| send verification link to email | | GET | /email_verification | | verify token and change password |

(more details in the code)

Module api

  • authApi.init(config): configure the module
  • authApi.authRouter: an express router I wrote for you
  • authApi.verifyToken: an express middleware used to verify token sent by client

TODOS

  • [ ] better http status code
  • [ ] better config params
  • [ ] docs
  • [ ] new feature

license

MIT

As a starter see the starter branch