This project demonstrates a very small amount of code to allow Next.js to authenticate requests on both the server and the client. This is done by saving a JWT in a cookie on the Next.js host.
The JWT is created by a separate Micro server (server/index.js
).
The Next app has two pages.
index.js
, where the user can login and logout.about.js
, which requires the user to be logged in (and redirects to/index
if not).
The Micro server has two methods. The idea would be to replace this with your own, smarter auth server.
/login
, which always succeeds and returns{ jwt: "...", user: { id: "..." } }
./me
, which only succeeds if there is anAuthorization: Bearer ...
header with a valid JWT.
To start the Next.js server:
yarn
next dev
To start the Micro server:
cd server/
yarn
micro-dev -p 3004 # localhost:3004 is the default server address