ProSignup Logo

pro-signup

Authentication module for express

What is ProSignup?

ProSignup is a node module that handles the user authentication for express applications. It adds two REST APIs POST: /login and POST: /register and creates a collection User in MongoDB to manage users.

Quick Start

Prerequisites

Usage

  1. Install pro-signup module
     npm install pro-signup --save
    
  2. Integrate with your express application
     const express = require('express')
     const proSignup = require('pro-signup')({
         jwtSecret: process.env.jwtSecret
     })
     const app = express()
    
     // connect to MongoDB here
    
     app.use('/auth', proSignup.router)
     app.get('/profile', proSignup.ensureAuthenticated, function (req, res) {
         let email = res.locals.user.email;
         res.send('some private data for user ' + email);
     })
     app.get('/', function (req, res) {
         res.send('some public data without authentication')
     })
    
     app.listen(3000)
    

Verifying that it works

GET: http://localhost:9000/                 RESPONSE: some public data without authentication
GET: http://localhost:9000/profile          RESPONSE: { "redirect": "/login" }
POST /auth/register HTTP/1.1
Host: localhost:9000
Content-Type: application/x-www-form-urlencoded

name=user1&password=thisispassword123!&password2=thisispassword123!&email=user1@email.com
POST /auth/login HTTP/1.1
Host: localhost:9000
Content-Type: application/x-www-form-urlencoded

password=thisispassword123!&email=user1@email.com

and after logging in:

GET: http://localhost:9000/profile          RESPONSE: some private data for user user2@email.com

Detailed Guide

User Model

ProSignup creates a new collection with following fields in MongoDB

{
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        lowercase: true,
        unique: true
    },
    password: {
        type: String, //hashed
        required: true
    },
    date: {
        type: Date,
        default: Date.now()
    }
}

Configuration

You need to provide a JSON object with configuration for it to work.

const proSignup = require('pro-signup')({
    //configuration parameters
})

Configuration parameters

Methods

app.get('/protected-route', proSignup.ensureAuthenticated, function (req, res) {
    let email = res.locals.user.email;
    res.send('some private data for user ' + email);
})
app.use('/protected-routes', proSignup.ensureAuthenticated, someRouterInstance)

REST APIs

/register

POST /basename/register HTTP/1.1
Content-Type: application/x-www-form-urlencoded

name=name of the user&password=thisispassword123!&password2=thisispassword123!&email=useremail@domain.com

on success

{
    "status": true,
    "redirect": "/login"
}

on error

{
    "errors": [
        {
            "msg": "error description"
        },
        ...
    ]
}

/login

POST /auth/login HTTP/1.1
Content-Type: application/x-www-form-urlencoded

password=thisispassword123!&email=useremail@domain.com

on success ( plus sets a cookie ‘checksum=jsonwebtoken’ )

{
    "status": true,
    "redirect": "/"
}

on error

{
    "errors": [
        {
            "msg": "error description"
        },
        ...
    ]
}

Upcoming features