├── .babelrc
├── .gitignore
├── LICENSE
├── README.md
├── package.json
├── pages
├── index.js
└── login.js
├── server.js
└── utils
└── CookieUtils.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "next/babel",
4 | "es2015",
5 | "stage-3"
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | .next/
3 | npm-debug.log
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Nhan Tran
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [Demo](https://nextjs-simple-authentication-with-jwt-dgjcfwbaax.now.sh)
2 |
3 | # The idea behind
4 | The very basic auththentication implemetation for next.js with Jsonwebtoken. Basically, we use expressjs to customize the server side, by adding an authentication middleware, this middleware will check the `x-access-token` in every request comming to server.
5 |
6 | We also have a endpoint to generate jwt token and this token will be stored in cookies. I find this way is simple and work very well, especially in the time next.js haven't had an official example for this yet.
7 |
8 | # How to use
9 |
10 | 1. Clone or download
11 | 2. `npm install`
12 | 3. `npm run build`
13 | 4. `npm start`
14 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "next.js-simple-authentication-with-jwt",
3 | "version": "1.0.0",
4 | "description": "Simple authentication with Jsonwebtoken for nextjs",
5 | "main": "server.js",
6 | "scripts": {
7 | "build": "next build && babel ./ --ignore ./node_modules,./dist -d dist",
8 | "dev": "babel-node server.js",
9 | "start": "NODE_ENV=production node dist/server.js"
10 | },
11 | "author": "nhantran",
12 | "license": "MIT",
13 | "dependencies": {
14 | "axios": "^0.16.2",
15 | "body-parser": "^1.17.2",
16 | "cookie-parser": "^1.4.3",
17 | "crypto": "0.0.3",
18 | "express": "^4.15.3",
19 | "jsonwebtoken": "^7.4.1",
20 | "jwt-decode": "^2.2.0",
21 | "next": "^3.0.1-beta.8",
22 | "react": "^15.6.1",
23 | "react-dom": "^15.6.1"
24 | },
25 | "devDependencies": {
26 | "babel-cli": "^6.24.1",
27 | "babel-preset-es2015": "^6.24.1",
28 | "babel-preset-stage-3": "^6.24.1"
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/pages/index.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react'
2 | import Router from 'next/router'
3 | import jwtDecode from 'jwt-decode';
4 | import { setCookie, getCookie } from '../utils/CookieUtils'
5 | import axios from 'axios'
6 |
7 | export default class Index extends Component {
8 | constructor (props) {
9 | super(props)
10 | this.state = {
11 | message: ''
12 | }
13 | this.logout = (e) => this._logout()
14 | this.testCSRF = (e) => this._testCSRF()
15 |
16 | }
17 | _logout () {
18 | setCookie('x-access-token', '')
19 | Router.push({
20 | pathname: '/login'
21 | })
22 | }
23 | // Use javascript to get jwt-token in cookies which can only
24 | // done by JavaScript that runs on your domain can read the cookie
25 | // You can override axios request config for general solution
26 | async _testCSRF () {
27 | const token = getCookie('x-access-token')
28 | const decoded = jwtDecode(token)
29 | try {
30 | const res = await axios.post(window.location.origin + '/api/preventCRSF', {
31 | example: 'data'
32 | }, {
33 | headers: {
34 | 'X-XSRF-TOKEN': decoded.xsrfToken
35 | }
36 | })
37 | if (res.data.success) {
38 | this.setState({
39 | message: res.data.message
40 | })
41 | }
42 | } catch (error) {
43 | this.setState({
44 | message: error.response.data.message
45 | })
46 | }
47 | }
48 | render () {
49 | return (
50 |