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

nodeauthapi

v1.0.7

Published

This is an authapi lib. which can work as middleware for authentication. You can use it directly without creating model for database and controllers and session manager for this. You just config the database and forget.

Downloads

4

Readme

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