1.0.4 • Published 12 months ago

simple-next-csrf v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

simple-next-csrf

This project aims to add Cross-Site Request Forgery (CSRF) protection to a Next.js application that does have server-side props support.

Installation

npm install simple-next-csrf

Usage

create a file named csrf.js

import { simpleNextCSRF } from "simple-next-csrf";

const { csrfEnjector, csrfValidator } = simpleNextCSRF({
  secret: "hello-world",
  // your secret from environment variable
});

export { csrfEnjector, csrfValidator };

in page to enject CSRF token wrap getServerSideProps in csrfEnjector

import { csrfEnjector } from "../csrf";

export default function Home() {

  return (
    <main>
      ....
    </main>
  );
}

export const getServerSideProps = csrfEnjector((context) => {
  return {
    props: {},
  };
});

Wrap api handler in csrfValidator

import { csrfValidator } from "../../csrf";

function handler(req, res) {
  res.status(200).json({ name: "John Doe" });
}

export default csrfValidator(handler);
1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago