- Framework: Next.js
- useCase:
- Edge Functions
- Edge Middleware
- Documentation
- css: Tailwind
- Demo: https://311-data-v2-jwt.vercel.app/
- relatedTemplates:
- nextjs-boilerplate
- blog-starter-kit
- platforms-starter-kit
The example shows how to do JWT authentication at the edge using Edge Middleware and Edge Functions.
- Initially, you should not be authorized to view request data.
Visit: https://311-data-v2-jwt.vercel.app/api/requests?startDate=2023-04-01&endDate=2023-04-20 - Get authorized.
Click the button on this page: https://311-data-v2-jwt.vercel.app/ - Now you should be authorized to view the request data.
Visit and reload: https://311-data-v2-jwt.vercel.app/api/requests?startDate=2023-04-01&endDate=2023-04-20 - To revoke authorization, remove the
user-token
cookie from the client browser.
(Dev Tools > Application > Storage > Cookies) - Now, you should no longer be authorized to view the request data.
Visit and reload: https://311-data-v2-jwt.vercel.app/api/requests?startDate=2023-04-01&endDate=2023-04-20
You can choose from one of the following two methods to use this repository:
After setting up your JWT secret, deploy the example using Vercel:
Execute create-next-app
with npm or Yarn to bootstrap the example:
pnpm create next-app --example https://github.com/vercel/examples/tree/main/edge-middleware/jwt-authentication
Rename .env.example
to .env.local
:
cp .env.example .env.local
then, update JWT_SECRET_KEY
with your a random secret that'll be used to sign JWTs.
Next, run Next.js in development mode:
pnpm dev
The app should be up and running at http://localhost:3000.
Deploy it to the cloud with Vercel (Documentation).