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-next-auth

v3.0.15

Published

This is for next.js app to be used as jwt authratzation

Downloads

1,108

Readme

jwt-next-Auth

This module is used for creating jwt based auth in your next js application

To set SECRET_AUTH (consumed by jasonwebtoken). please set environmental variable SECRET_AUTH

To set Cookie encryption set enviromnetal Variable JWT_ALGO_256_ENCRYPT_KEY || crypto.randomBytes(16).toString("hex") JWT_ALGO_256_IV_KEY || crypto.randomBytes(8).toString("hex")

To check the implimentation of the module please check from example/test folder from out github repo https://github.com/umeshramya/jwt-next-auth.git

This has following API end points

  1. jwtSign This is for sign in

  2. IsPageLogged This is passed in getServerSideProps method with resolved promise on valied jasonwebtoken of signin else reject promise

  3. validateUser This is used to check the user for subesuqent protected routes

  4. jwtTokenCreate With this one can create new token for other uses in your application appilcatoin

  5. Login route example of login route in next

  6. Protected route This is closer for routes of api allowing roles as string of array passed and route function as argument see code below

  7. logout This sets token of signin "" thus user is logged out

  8. jwtverify Helper function to check jsonwebtoken

jwrSign

in you api routes

import {jwtSign} from "jwt-next-auth"

const route = async(req, res) => {
  try {
    let result = await jwtSign(req.body, req , res).then(res=>res);
    console.log(req.body)
    res.status(200).json({mes : result})
  } catch (error) {
    res.status(500).send(error)
  }
}

export default route;

IsPageLogged

in your page

import Head from 'next/head'
import {IsPageLogged} from "jwt-next-auth"


export default function Home(props) {
  return (
   <>
   <h1>{props.pageLogged ? "Page is logged" : "Page is not logged"}</h1>
        // your code of page goes here
   </>
  )
}


export async function getServerSideProps(ctx) {
  try {

    const result = await IsPageLogged(ctx.req, ctx.res).then(result=>result)
    return {
      props: {pageLogged : true}, // will be passed to the page component as props
    }
  } catch (error) {
 
    return { props: {pageLogged : false} };
  }
 
}

validateUser

in your api route

import {validateUser} from "jwt-next-auth"
const route = async(req, res)=>{
    try {
      await validateUser(req, res).then(r=>r)
        res.status(200).json({mes:JSON.stringify(req.body)})

    } catch (error) {
        res.status(500).send(error)
    }
}


export default route

jwtTokenCreate

In your route

import {jwtTokenCreate} from "jwt-next-auth"

const route = async(req, res)=>{
    try {
         let token = await jwtTokenCreate(req.body, 7).then(r=>r)
         res.status(200).json({mes:token})
    } catch (error) {
        res.status(500).send(error)
    }
}


export default route;

Login route of next


import {jwtSign} from "jwt-next-auth"

const route = async(req, res) => {
  try {
    let payload = req.body;
    //write code check from data base usernam and pasword
    // then add role property for the payload most ofetn dervied from database
    payload.role = "admin"// real world application this comes from database of users
    let result = await jwtSign(payload, req , res).then(res=>res);

    res.status(200).json({mes : result})
  } catch (error) {
    res.status(500).send(error)
  }
}

Protected route code

// this routes are inside api folder of pages of next js app
import { protectedRouteMaster} from "jwt-next-auth"


const route = async(req, res, body, auth) => {
    try {
        console.log("body", body)//access body requet
        console.log("auth", auth)//access auth body from here
        res.status(200).json({mes:"varied user"})
        
    } catch (error) {
        res.status(500).send(error)
    }
}


//array of strings second arguments it extrcts the role of payload set durring the login route see above
export default protectedRouteMaster(route, ["admin", "editor"])

logout

in your route

import {logout} from "jwt-next-auth"
const route = (req, res)=>{
    try {
        logout(req, res)
        res.status(200).json({mes: "logged out"})
    } catch (error) {
        res.status(500).send(error)
    }
}


export default route

jwtverify

in you api routes async method

jwtverify(tokren)