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

jwt-login

v0.0.3

Published

This is javascript web token for login with JWT and validadation

Downloads

84

Readme

jwt-login

verson License

This javascript node module. It is for creating javascript web token and also validating it. under the hood it uses crypto-js

Usage

Following are the Methods in this module

  1. setSecretKey
  2. createJWT
  3. validateJWT
  4. sign
  5. signout
  6. validate_login

include in your project

    var JWT = require("jwt-login")

setSecretKey Method

  1. It sets the secrete key. if not set, it uses defualt one
  2. Always set secreate key before createJWT method. Do not change it later in your code
    JWT.setSecretKey();// this sets new secret key 

createJWT Method

  1. This methods creates the JWT token
  2. it uses secret key
  3. payload argument has to be passed
  4. header argument is optional
    var payload = {"user" : username, "expDate" : Date}
    //header second is optional.
    var token = JWT.createJWT(payload, header)// returns the JWTtoken

validateJWT Method

  1. This returns the payload in case validation was sucusseful.
  2. for unsuccessful validatation it returns false.
  3. Argument is JWT. This JWT token is the one recived from client application.
    var valid = validateJWT(JWT);
    

express js example

const express = require("express")
const httpMsgs = require("http-msgs");
const jwtLogin = require("jwt-login");
const bodyparser = require("body-parser");
const app = express();

app.listen(9000);

app.use(bodyparser.urlencoded({extended : false}));

//login html file
app.get("/login", function(req, res){
    res.sendFile(__dirname + "/login.html");
});

//login
app.post("/login",  function(req, res){
    var user = req.body.user
    var password = req.body.password
    if (user == password){
        jwtLogin.sign(req, res, user,"topsecret", 1, false);  
    }else{
        httpMsgs.send500(req, res, "invalid user");
    }
    
});
// logout
app.get("/logout", function(req, res){
    jwtLogin.signout(req, res, false);
});

var valid_login = function(req, res, next){
    try {
        req.jwt = jwtLogin.validate_login(req, res);
        next();
    } catch (error) {
        httpMsgs.send500(req, res, error);
        
    }

}


/*
===============================
    routes 
==============================

*/ 

app.get("/article", valid_login, function(req, res){
    var user = req.jwt.user//this the user 
    httpMsgs.sendJSON(req, res,{
        from    : "get"
    });
});

app.post("/article", function(req, res){
    httpMsgs.sendJSON(req, res,{
        from    : "post"
    });
});