@adminforth/user-sessions

中文文档
2.6.0发布于 2026/11/07MITTypeScriptES Module近 30 天下载 1820

AdminForth plugin which shows active sessions of admin user and allows to revoke them

原始使用文档
文档目录4 节
  1. Installation
  2. How it works
  3. Options
  4. Development

AdminForth User Sessions plugin

Shows admin user their active sessions (one per browser they signed in from) and lets them revoke any of those sessions. Logging out revokes the session as well, so its auth token stops working immediately instead of staying valid until it expires.

Full documentation: adminforth.dev/docs/tutorial/Plugins/user-sessions

Installation

npm i @adminforth/user-sessions @adminforth/key-value-adapter-redis --save
import UserSessionsPlugin from '@adminforth/user-sessions';
import RedisKeyValueAdapter from '@adminforth/key-value-adapter-redis';

export const admin = new AdminForth({
  ...
  globalPlugins: [
    new UserSessionsPlugin({
      keyValueAdapter: new RedisKeyValueAdapter({ redisUrl: process.env.REDIS_URL }),
    }),
  ],
  ...
});

Sessions then appear under "Active sessions" in the user menu settings.

To let a privileged user see and revoke sessions of other users, add canManageOtherUsersSessions. The same block then appears on the show page of the users resource:

new UserSessionsPlugin({
  keyValueAdapter,
  canManageOtherUsersSessions: async (adminUser) => adminUser.dbUser.role === 'superadmin',
})

Without this option, sessions of other users are not shown and can't be revoked by anybody.

How it works

  • AdminForth puts a unique session id into every issued auth JWT.
  • auth.afterSessionCreated hook stores <user pk>:<session id> in the key-value adapter, with the same expiration as the token, holding ip, country, user_agent, created_at and last_used_at.
  • auth.adminUserAuthorize hook checks that the session of the request is still stored, and refreshes its last_used_at. A session which is not stored is rejected with 401.
  • auth.beforeLogout hook and the revoke endpoints delete the stored session, and delete it once more 5 seconds later: a request running at that moment may have read the session just before and write it back while refreshing last_used_at.

Because a missing record means "revoked", the store must outlive the app process: use Redis (or another shared, persistent adapter) in production. With an in-memory adapter every restart signs all users out. Installing the plugin also signs out everyone who was logged in before it.

Requests authorized without a session id — MCP api keys, or tokens issued before the plugin was installed — are left alone.

Options

OptionDefaultDescription
keyValueAdapterrequiredWhere active sessions are stored
collectionadminforth-user-sessionsCollection (key prefix) used in the adapter
countryHeaderCF-IPCountryHeader with two-letter country code set by your CDN. Empty string disables it
canManageOtherUsersSessions—(adminUser) => Promise<boolean>. When it returns true, that user sees and can revoke sessions of other users on the users show page
resolveCountry—({ ip, headers }) => Promise<string | null>, called when the header did not tell the country
lastUsedThrottleSeconds60How often last_used_at is refreshed, one adapter write per refresh

The Device column shows the browser and OS parsed from the stored User-Agent (e.g. Chrome 131 on macOS) with a desktop/mobile/tablet icon. The raw header is stored as it came, so parsing always runs on the current plugin version.

Client IP is taken from adminforth.auth.getClientIp, so configure auth.clientIpHeader when running behind a proxy — otherwise the IP is not trustworthy and is null for private addresses.

Development

pnpm i
# hooks this plugin uses are not released yet, so point it at a local adminforth checkout
# (every `pnpm i` resets this link back to the published package)
pnpm link ../../adminforth
pnpm test
pnpm build

版本与发布记录

@adminforth/user-sessions 共发布 18 个版本,首次发布于 2026/09/21;过去 12 个月发布了 18 个版本。

版本发布日期距今
2.6.0latest2026/11/071 天前
2.5.02026/11/044 天前
2.4.02026/11/034 天前
2.3.12026/11/035 天前
2.3.02026/11/035 天前
2.2.12026/11/035 天前

关于 @adminforth/user-sessions 的常见问题

@adminforth/user-sessions 是什么?

AdminForth plugin which shows active sessions of admin user and allows to revoke them

如何安装 @adminforth/user-sessions?

在项目目录运行 npm install @adminforth/user-sessions,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:adminforth(^4.24.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@adminforth/user-sessions 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adminforth/user-sessions 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adminforth/user-sessions 可以商用吗?

@adminforth/user-sessions 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adminforth/user-sessions 的下载量有多少?

根据 npm 官方下载统计,@adminforth/user-sessions 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,820 次,日均约 61 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adminforth/user-sessions 还在维护吗?

最新版本 2.6.0 发布于 2026/11/07(1 天前);至今共发布 18 个版本,其中过去 12 个月发布了 18 个;首次发布于 2026/09/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。