-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from j0lv3r4/bug-fixes-documentation
Update functionality, docs, and bug fixes
- Loading branch information
Showing
23 changed files
with
510 additions
and
9,717 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,4 +151,4 @@ dist | |
.idea | ||
|
||
.now | ||
example/.vercel | ||
example/.vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CSRF_SECRET="P*3NGEEaJV3yUGDJA9428EQRg!ad" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { nextCsrf } from "next-csrf"; | ||
|
||
const options = { | ||
// eslint-disable-next-line no-undef | ||
secret: process.env.CSRF_SECRET, | ||
}; | ||
|
||
export const { csrf, setup, csrfToken } = nextCsrf(options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "next-csrf-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"next": "9.5.3", | ||
"react": "16.13.1", | ||
"react-dom": "16.13.1" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^2.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import "../styles/globals.css"; | ||
|
||
function MyApp({ Component, pageProps }) { | ||
return <Component {...pageProps} />; | ||
} | ||
|
||
export default MyApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { setup } from "../../../lib/csrf"; | ||
|
||
const handler = (req, res) => { | ||
res.statusCode = 200; | ||
res.json({ message: "CSRF token added to cookies" }); | ||
}; | ||
|
||
export default setup(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
|
||
export default (req, res) => { | ||
res.statusCode = 200; | ||
res.json({ name: "John Doe" }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
import { csrf } from "../../lib/csrf"; | ||
|
||
const handler = (req, res) => { | ||
res.statusCode = 200; | ||
res.json({ message: "Request successful" }); | ||
}; | ||
|
||
export default csrf(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Head from "next/head"; | ||
import styles from "../styles/Home.module.css"; | ||
import { setup } from "../lib/csrf"; | ||
|
||
export default function Home() { | ||
// We send a request to setup the csrf token | ||
// fetch("http://localhost:3000/api/csrf/setup") | ||
// .then((response) => { | ||
// console.log(response); | ||
// if (response.ok) { | ||
// console.log("response ok"); | ||
// console.log("csrf token setup correctly"); | ||
// // console.log("cookies", document.cookie); | ||
// } | ||
// }) | ||
// .catch((error) => console.error(error)); | ||
|
||
const requestWithToken = () => | ||
fetch("/api/protected", { | ||
method: "post", | ||
}) | ||
.then((response) => { | ||
if (response.ok) { | ||
console.log("protected response ok"); | ||
console.log(response); | ||
} | ||
}) | ||
.catch((error) => console.error(error)); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<Head> | ||
<title>Next CSRF</title> | ||
<link rel="icon" href="/favicon.ico" /> | ||
</Head> | ||
|
||
<main className={styles.main}> | ||
<h1 className={styles.title}>Next CSRF</h1> | ||
|
||
<p className={styles.description}> | ||
Get started by editing{" "} | ||
<code className={styles.code}>pages/index.js</code> | ||
</p> | ||
|
||
<div> | ||
<div className={styles.card}> | ||
<h3>Send a request with a valid CSRF token</h3> | ||
|
||
<p> | ||
Open the Web Console and click in the button below to see how a | ||
valid request works. | ||
</p> | ||
|
||
<button className={styles.button} onClick={requestWithToken}> | ||
With CSRF token | ||
</button> | ||
</div> | ||
|
||
<div className={styles.card}> | ||
<h3>Send a request without the CSRF token</h3> | ||
|
||
<p> | ||
Because any request we send from the browser will have a cookie | ||
with the token attached, try to send a request from a terminal and | ||
see what happens with a missing or an invalid CSRF token. | ||
</p> | ||
|
||
<pre> | ||
<code className={styles.code}> | ||
$ curl -X POST http://localhost:3000/api/protected | ||
</code> | ||
</pre> | ||
|
||
<pre> | ||
<code className={styles.code}> | ||
{`>> {"message": "Invalid CSRF token"}`} | ||
</code> | ||
</pre> | ||
</div> | ||
</div> | ||
</main> | ||
|
||
<footer className={styles.footer}> | ||
<a | ||
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Powered by{" "} | ||
<img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} /> | ||
</a> | ||
</footer> | ||
</div> | ||
); | ||
} | ||
|
||
export const getServerSideProps = setup(async () => { | ||
return { props: {} }; | ||
}); |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.