
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
@iamjs/next
Advanced tools
This package contains server-side components for integrating the authorization library into a Next.js application.
@iamjs/next
This package contains the next middleware for iamjs a library for easy role and permissions management for your next application.
npm install @iamjs/core @iamjs/next
# or
yarn add @iamjs/core @iamjs/next
# or
pnpm add @iamjs/core @iamjs/next
# or
bun add @iamjs/core @iamjs/next
You can use the NextRoleManager to authorize a request in your express application by creating a new instance and using the check method.
Example:
import { Role, Schema } from '@iamjs/core';
import { NextRoleManager } from '@iamjs/next';
import next from 'next';
const role = new Role({
name: 'role',
config: {
resource1: {
base: 'crudl'
},
resource2: {
base: 'cr-dl',
custom: {
'create a new user': false
}
}
}
});
const schema = new Schema({
roles : { role }
});
const roleManager = new NextRoleManager({
schema: schema,
onSuccess: (req, res) => {
res.status(200).send('Hello World!');
},
onError: (err, req, res) => {
console.error(err);
res.status(403).send('Forbidden');
}
});
const handler = roleManager.check(
(_req, res) => {
res.status(200).send('Hello World!');
},
{
resources: 'resource2',
actions: ['create', 'update'],
role: 'role',
strict: true
});
export default handler
By using the construct option you can use the data from the request to build the role from its own permissions.
Example:
import { Role, Schema } from '@iamjs/core';
import { NextRoleManager } from '@iamjs/next';
import next from 'next';
const role = new Role({
name: 'role',
config: {
resource1: {
base: 'crudl'
},
resource2: {
base: 'cr-dl',
custom: {
'create a new user': false
}
}
}
});
const schema = new Schema({
roles : { role }
});
const roleManager = new NextRoleManager({
schema: schema,
onSuccess: (req, res) => {
res.status(200).send('Hello World!');
},
onError: (err, req, res) => {
console.error(err);
res.status(403).send('Forbidden');
}
});
const withAuth = (handler) => {
return (req, res) => {
req.permissions = role.toObject();
return handler(req, res);
};
};
const handler = roleManager.check(
(_req, res) => {
res.status(200).send('Hello World!');
},
{
resources: 'resource2',
actions: ['create', 'update'],
strict: true,
construct: true, // to use the data from the request to build the role's permissions
data: async (req)=>{
return req.permissions
}
});
export default withAuth(handler);
The next.js 13 new api routes are using the browser's fetch api so you can't use the middleware with pages
request and response interfaces but you can use the checkFn function to check if the user is authenticated
inside the actual route handler.
import { getUserPermissions } from '@lib/utils/auth';
import { roleManager } from '@lib/utils/api';
export async function GET(request: Request) {
const authorized = await roleManager.checkFn({
resources: 'resource2',
actions: ['read'],
strict: true,
construct: true,
data: async () => {
return await getUserPermissions(request); // handle getting user permissions
}
});
if (!authorized) {
return new Response('Unauthorized', { status: 401 });
}
return new Response('Authorized', { status: 200 });
}
You can pass onSuccess and onError handlers to the NextRoleManager constructor to handle the success and error cases.
Example:
import { Role, Schema } from '@iamjs/core';
import { NextRoleManager } from '@iamjs/next';
import next from 'next';
const role = new Role({
name: 'role',
config: {
resource1: {
base: 'crudl'
},
resource2: {
base: 'cr-dl',
custom: {
'create a new user': false
}
}
}
});
const schema = new Schema({
roles : { role }
});
const roleManager = new NextRoleManager({
schema: schema,
onSuccess: (req, res) => {
res.status(200).send('Hello World!');
},
onError: (err, req, res) => {
console.error(err);
res.status(403).send('Forbidden');
}
});
const withAuth = (handler) => {
return (req, res) => {
req.permissions = role.toObject();
return handler(req, res);
};
};
const handler = roleManager.check(
(_req, res) => {
res.status(200).send('Hello World!');
},
{
resources: 'resource2',
actions: ['create', 'update'],
strict: true,
construct: true, // to use the data from the request to build the role's permissions
data: async (req)=>{
return req.permissions
}
});
export default withAuth(handler);
This package is written in typescript and has type definitions for all the exported types also the check method accepts a generic type which can be used to define the type of the request or response object.
Example:
interface Request extends NextApiRequest {
role: string;
permissions: Record<string, Record<permission, boolean>>;
}
interface Response extends NextApiResponse {}
import { Role, Schema } from '@iamjs/core';
import { NextRoleManager } from '@iamjs/next';
import next from 'next';
const role = new Role({
name: 'role',
config: {
resource1: {
base: 'crudl'
},
resource2: {
base: 'cr-dl',
custom: {
'create a new user': false
}
}
}
});
const schema = new Schema({
roles : { role }
});
const roleManager = new NextRoleManager({
schema: schema,
onSuccess: <Request,Response>(req, res) => {
res.status(200).send('Hello World!');
},
onError: <Request,Response>(err, req, res) => {
console.error(err);
res.status(403).send('Forbidden');
}
});
const withAuth = (handler) => {
return (req, res) => {
req.permissions = role.toObject();
return handler(req, res);
};
};
const handler = roleManager.check<Request,Response>(
(_req, res) => {
res.status(200).send('Hello World!');
},
{
resources: 'resource2',
actions: ['create', 'update'],
strict: true,
construct: true, // to use the data from the request to build the role's permissions
data: async (req)=>{
return req.permissions
}
});
export default withAuth(handler);
You can save user activity using the onActivity method on the NextRoleManager
import { Role, Schema } from "@iamjs/core";
import { NextRoleManager } from "@iamjs/next";
import next from "next";
const role = new Role({
name: "role",
config: {
resource1: {
base: "crudl",
},
resource2: {
base: "cr-dl",
custom: {
"create a new user": false,
},
},
},
});
const schema = new Schema({
roles: { role },
});
const roleManager = new NextRoleManager({
schema: schema,
onSuccess: (req, res) => {
res.status(200).send("Hello World!");
},
onError: (err, req, res) => {
console.error(err);
res.status(403).send("Forbidden");
},
async onActivity(data) {
console.log(data); // the activity object
},
});
const withAuth = (handler) => {
return (req, res) => {
req.permissions = role.toObject();
return handler(req, res);
};
};
const handler = roleManager.check(
(_req, res) => {
res.status(200).send("Hello World!");
},
{
resources: "resource2",
actions: ["create", "update"],
strict: true,
construct: true, // to use the data from the request to build the role's permissions
data: async (req) => {
return req.permissions;
},
}
);
export default withAuth(handler);
The data object contains:
| Name | Description |
|---|---|
| actions? | The action or actions that are authorized to be executed on the resource |
| resources? | The resource or resources that are authorized to be accessed |
| role? | The role that is used to authorize the request |
| success? | The status of the request |
| req? | The request object |
FAQs
This package contains server-side components for integrating the authorization library into a Next.js application.
The npm package @iamjs/next receives a total of 6 weekly downloads. As such, @iamjs/next popularity was classified as not popular.
We found that @iamjs/next demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.