davieoba / nextjs-middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to protect API routes with Nextjs 13 middleware

First, run the development server:

npm run dev
# or
yarn dev
# or
pnpm dev

Open Postman or any other Rest API client and run the requests in the src > app > api directory.

For example, src > app > api > admin > list > route.ts will be http://localhost:3000/api/admin/list. And so on.

Don't forget to pass Authorization headers of role or admin in your request, depending on the permission that each role has.

If you don't pass an Authorization header, you'll get a 401 unauthorized. If you pass a wrong role, you'll get a 401 unauthorized.

Edit the middleware.ts file to update permissions.

Hack away!

About


Languages

Language:TypeScript 92.7%Language:CSS 5.5%Language:JavaScript 1.8%