Giter VIP home page Giter VIP logo

nextjs-jwt-authentication's Introduction

Next.js JWT Authentication

A proof of concept app for demonstrating authentication of Next.js app with JWT.

The app is made under the assumption that the rendering server and the API server are decoupled.

It uses this API for authentication.

Demo

Idea:

Server side rendered apps are awesome. They make a sweet spot between monolithic apps and single page apps powered by microservices. But they also add complexity to their creation. Keeping state on the server and the client in sync, routing on the client and the server, loading data before server rendering, etc. are some of the things that make our apps more complex. That is why frameworks such as Next.js exist, to solve most of those problems. However, things such as authentication are left to us. This is an attempt to implement the authentication based on JWT. The thing is that we can't store tokens in localStorage as we would do with SPA.

The idea is to receive token from the server on the client, store it in cookies, and then whenever a page renders on the server, we would be able to access the token from the cookie.

How it works:

The client:

The user sends an auth request from the client. In the response, JWT is received, and stored in browser cookies and redux store. Then the user has access to the token from the client, and can use it to access protected routes.

The server:

When the user makes a page request, cookies are also sent along, so the server can read them. To read it, the user is using the getInitialProps lifecycle method, provided by Next.js. It gets an argument - the context object that contains some properties. You can read more about it here. On the req property, we can access headers property which contains the cookie with the token. An action creator is dispatched to save the token in the redux store, and the app now has access to the token again.

Usage:

Installation:

Make sure you have Node and NPM installed.

Also make sure you have the API server running.

git clone https://github.com/alan2207/nextjs-jwt-authentication
cd nextjs-jwt-authentication
npm install

Starting the app:

# Run in dev mode:
npm run dev

# Run in production:
npm run build
npm start

nextjs-jwt-authentication's People

Contributors

alan2207 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nextjs-jwt-authentication's Issues

Inverted Reducers and Actions ?

May I ask. Didn't u invert Redux Actions and Reducers ?

I mean. Actions are supposed to be objects containing the payloads reducers "calculated" ?

Sorry, this probably doesn't deserve an "Issue report".

Typescript Monorepo Version

Hey Alan, thanks a lot for this repo and the express example.
I have rewritten your project in TypeScript as a monorepo, for my purposes.
You could Link my project if someone searches for a TypeScript Version of this project.
Cheers

Question: how to do a redirect if not logged in

First, thanks for this repo - big help!

I was looking at the whoami.js and wondering how exactly you'd do a redirect? Just as the else of the if (token) in the Whoami.getInitialProps? Or is it more complicated than that?

Local Storage

Is it possible to save the token in localStorage ?

Error with command: npm start

PS C:\Users\p-c\Downloads\Github\nextjs-jwt-authentication> npm start

[email protected] start
NODE_ENV=production node server.js

'NODE_ENV' n’est pas reconnu en tant que commande interne
ou externe, un programme exécutable ou un fichier de commandes.

Remove Cookie

Same parameters are needed to be passed with cookie.remove() that where pass while cookie.set()

ie.

cookie.set(key, value, {
			expires: 1,
			path: '/',
		});

cookie.remove(key, {
			expires: 1,
			path: '/', //==> Add path as well. Else cookie wont be deleted
		});

How to disable eslint

Hi Thanks for this project really appreciate it. Please how do I disable eslint? It's causing me a whole lot of troubles. 👍 thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.