Giter VIP home page Giter VIP logo

nodeauthapi's Introduction

nodeAuthApi

This library use for authentication work as middleware. As of now you can add login feature to your app. Some feature like authenticate, manage authenticated URL and unauthenticated URL etc.. This API is totally based on local strategy (i.e. you can authenticate using only your database not 3rd party authentication).

Install

    $ npm i nodeauthapi 

Usage

Create application (config)

Before using this API you have to initialize any app. We are using the Express framework for explaining this API. To initialize the Express app follow the following commands:

    $ express --version
    $ cd ~/Desktop
    $ express --view=ejs myapp
    $ cd myapp
    $ npm install
    $ npm install nodeauthapi
Configure database and authentication

For configuring the database and authentication API have a function config() in manager.js. To configure the database pass fist parameter in as config('db',options) 4 and for authentication config pass config('auth',options), options are different for both configuration.
Follow the following code:

const nodeAuthApi = require('nodeauthapi');
//for database config
nodeAuthApi.config('db',{
    dbURI:'mongodb://127.0.0.1/test', //Your hosted mongodb URI,
    modelName:'users',//Name of model you want to create
    Schema : {
        //username, password and email are required
        username:{
            type : String,
            required:true
        },
        password:{
            type:String,
            required:true,
            bcrypt:true
        },
        email:{
            type:String,
            required:true
        },
        name:{
            type:String
        }
    } // Schema object is for database schema
});
    
//for authentication config
nodeAuthApi.config('auth',{
    sessionSecret : 'secret', // secret for json web token
    sessionExpirein : '1m' // session expire time
});

Configure the API before configuring any routers.

Authenticate Requests

Use nodeAuthApi.authenticate(), for authnticating the request. This function contain one parameter options and it is object containing successRediect, failureRedirect.
For example, as route middleware in an Express application:

app.post('/login', nodeAuthApi.authenticate({
    successRedirect : '/dashboard', // redirect to this route if successfully logged in
    failureRedirect : '/login' // redirect when fail to authenticate
}));
Other

Here we are discuss about other API like logout(),isAuthenticated(),isNotAuthenticated():

  • Use of logout()
//For logout the session
app.get('/logout',nodeAuthApi.logout({
    redirectURI:'/login' // URI for redirecting after logout
}));
  • Use of isAuthenticated()
//For verifying that user are authenticated are not
app.get('/dashboard',nodeAuthApi.isAuthenticated({
    failureRedirect : '/login' // redirect user when not authenticated
}),(req, res, next)=>{
    res.send('Protected page'); //User request jump to next middleware if user is authenticated
});
  • Use of isNotAuthenticated()
//For verifying that user are already logged in OR not
app.get('/dashboard',nodeAuthApi.isNotAuthenticated({
    failureRedirect : '/dashboard' // redirect user when authenticated
}),(req, res, next)=>{
    res.send('Login Page'); //User request jump to next middleware if user is not authenticated
});

License

The MIT License
Copyright (c) 2018 Shubham Srivastava

nodeauthapi's People

Contributors

shubham-sri avatar anmolgi1401 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.