Adds time-based one-time password verification as a second factor for a single auth-enabled Payload collection. It wraps existing access functions so TOTP must be verified before the original access logic runs, and adds setup and verify views to the admin panel. Requires a proxy or middleware that passes the pathname as a header, and should be the last plugin in the array.
Add an extra security layer to PayloadCMS using a Time-based One-time Password (TOTP).
Installation
npm i payload-totp168
Stars
23
Forks
0
Open Issues
1 months ago
Last Updated
24.7k
Weekly Downloads
v3.0.7
npm Version
375.0 KB
Unpacked Size
3
Dependencies
168+9 since Sep 6
| Date | Stars |
|---|---|
| Dec 5, 2026 | 168 |
| Dec 4, 2026 | 168 |
| Dec 3, 2026 | 168 |
| Dec 2, 2026 | 168 |
| Dec 1, 2026 | 168 |
| Nov 30, 2026 | 168 |
| Nov 29, 2026 | 168 |
| Nov 28, 2026 | 168 |
| Nov 27, 2026 | 168 |
| Nov 26, 2026 | 168 |
| Nov 25, 2026 | 168 |
| Nov 24, 2026 | 168 |
| Nov 23, 2026 | 168 |
| Nov 22, 2026 | 168 |
| Nov 21, 2026 | 168 |
| Nov 20, 2026 | 167 |
| Nov 19, 2026 | 167 |
| Nov 18, 2026 | 167 |
| Nov 17, 2026 | 167 |
| Nov 16, 2026 | 166 |
| Nov 15, 2026 | 166 |
| Nov 14, 2026 | 166 |
| Nov 13, 2026 | 166 |
| Nov 12, 2026 | 166 |
| Nov 11, 2026 | 165 |
| Nov 10, 2026 | 165 |
| Nov 9, 2026 | 164 |
| Nov 8, 2026 | 164 |
| Nov 7, 2026 | 164 |
| Nov 6, 2026 | 164 |
| Nov 5, 2026 | 164 |
| Nov 4, 2026 | 164 |
| Nov 3, 2026 | 164 |
| Nov 2, 2026 | 164 |
| Nov 1, 2026 | 163 |
| Oct 31, 2026 | 163 |
| Oct 30, 2026 | 163 |
| Oct 29, 2026 | 163 |
| Oct 28, 2026 | 163 |
| Oct 27, 2026 | 163 |
| Oct 26, 2026 | 163 |
| Oct 25, 2026 | 163 |
| Oct 24, 2026 | 163 |
| Oct 23, 2026 | 163 |
| Oct 22, 2026 | 163 |
| Oct 21, 2026 | 163 |
| Oct 20, 2026 | 163 |
| Oct 19, 2026 | 163 |
| Oct 18, 2026 | 163 |
| Oct 17, 2026 | 163 |
| Oct 16, 2026 | 163 |
| Oct 15, 2026 | 163 |
| Oct 14, 2026 | 163 |
| Oct 13, 2026 | 163 |
| Oct 12, 2026 | 163 |
| Oct 11, 2026 | 163 |
| Oct 10, 2026 | 163 |
| Oct 9, 2026 | 163 |
| Oct 8, 2026 | 163 |
| Oct 7, 2026 | 161 |
| Oct 6, 2026 | 161 |
| Oct 5, 2026 | 161 |
| Oct 4, 2026 | 161 |
| Oct 3, 2026 | 161 |
| Oct 2, 2026 | 161 |
| Oct 1, 2026 | 161 |
| Sep 30, 2026 | 161 |
| Sep 29, 2026 | 161 |
| Sep 28, 2026 | 161 |
| Sep 27, 2026 | 161 |
| Sep 26, 2026 | 161 |
| Sep 25, 2026 | 161 |
| Sep 24, 2026 | 160 |
| Sep 23, 2026 | 160 |
| Sep 22, 2026 | 160 |
| Sep 21, 2026 | 160 |
| Sep 20, 2026 | 160 |
| Sep 19, 2026 | 160 |
| Sep 18, 2026 | 160 |
| Sep 17, 2026 | 159 |
| Sep 16, 2026 | 159 |
| Sep 15, 2026 | 159 |
| Sep 14, 2026 | 159 |
| Sep 13, 2026 | 159 |
| Sep 12, 2026 | 159 |
| Sep 11, 2026 | 159 |
| Sep 10, 2026 | 159 |
| Sep 9, 2026 | 159 |
| Sep 8, 2026 | 159 |
| Sep 7, 2026 | 159 |
| Sep 6, 2026 | 159 |
24.4K+15.8K since Sep 6
| Date | Weekly downloads |
|---|---|
| Dec 5, 2026 | 24,404 |
| Dec 4, 2026 | 24,183 |
| Dec 3, 2026 | 23,458 |
| Dec 2, 2026 | 24,211 |
| Dec 1, 2026 | 24,278 |
| Nov 30, 2026 | 24,036 |
| Nov 29, 2026 | 25,856 |
| Nov 28, 2026 | 26,953 |
| Nov 27, 2026 | 27,578 |
| Nov 26, 2026 | 28,380 |
| Nov 25, 2026 | 29,495 |
| Nov 24, 2026 | 30,086 |
| Nov 23, 2026 | 30,560 |
| Nov 22, 2026 | 29,640 |
| Nov 21, 2026 | 29,169 |
| Nov 20, 2026 | 28,844 |
| Nov 19, 2026 | 27,869 |
| Nov 18, 2026 | 26,035 |
| Nov 17, 2026 | 25,696 |
| Nov 16, 2026 | 25,522 |
| Nov 15, 2026 | 24,114 |
| Nov 14, 2026 | 22,368 |
| Nov 13, 2026 | 20,860 |
| Nov 12, 2026 | 19,826 |
| Nov 11, 2026 | 19,479 |
| Nov 10, 2026 | 19,460 |
| Nov 9, 2026 | 18,608 |
| Nov 8, 2026 | 18,279 |
| Nov 7, 2026 | 18,348 |
| Nov 6, 2026 | 18,017 |
| Nov 5, 2026 | 17,428 |
| Nov 4, 2026 | 16,829 |
| Nov 3, 2026 | 16,255 |
| Nov 2, 2026 | 16,483 |
| Nov 1, 2026 | 16,598 |
| Oct 31, 2026 | 16,189 |
| Oct 30, 2026 | 15,803 |
| Oct 29, 2026 | 13,452 |
| Oct 28, 2026 | 10,784 |
| Oct 27, 2026 | 10,736 |
| Oct 26, 2026 | 11,338 |
| Oct 25, 2026 | 10,621 |
| Oct 24, 2026 | 10,160 |
| Oct 23, 2026 | 7,779 |
| Oct 22, 2026 | 10,529 |
| Oct 21, 2026 | 12,781 |
| Oct 20, 2026 | 12,717 |
| Oct 19, 2026 | 12,054 |
| Oct 18, 2026 | 12,216 |
| Oct 17, 2026 | 12,416 |
| Oct 16, 2026 | 14,293 |
| Oct 15, 2026 | 13,819 |
| Oct 14, 2026 | 13,436 |
| Oct 13, 2026 | 13,551 |
| Oct 12, 2026 | 13,257 |
| Oct 11, 2026 | 12,621 |
| Oct 10, 2026 | 12,722 |
| Oct 9, 2026 | 12,809 |
| Oct 8, 2026 | 12,533 |
| Oct 7, 2026 | 12,808 |
| Oct 6, 2026 | 12,633 |
| Oct 5, 2026 | 12,833 |
| Oct 4, 2026 | 14,256 |
| Oct 3, 2026 | 14,523 |
| Oct 2, 2026 | 19,487 |
| Oct 1, 2026 | 24,922 |
| Sep 30, 2026 | 27,627 |
| Sep 29, 2026 | 27,884 |
| Sep 28, 2026 | 28,651 |
| Sep 27, 2026 | 26,534 |
| Sep 26, 2026 | 24,850 |
| Sep 25, 2026 | 19,453 |
| Sep 24, 2026 | 13,791 |
| Sep 23, 2026 | 10,154 |
| Sep 22, 2026 | 9,322 |
| Sep 21, 2026 | 7,855 |
| Sep 20, 2026 | 13,445 |
| Sep 19, 2026 | 16,154 |
| Sep 18, 2026 | 16,005 |
| Sep 17, 2026 | 15,728 |
| Sep 16, 2026 | 15,796 |
| Sep 15, 2026 | 17,069 |
| Sep 14, 2026 | 17,463 |
| Sep 13, 2026 | 12,028 |
| Sep 12, 2026 | 9,443 |
| Sep 11, 2026 | 9,588 |
| Sep 10, 2026 | 9,292 |
| Sep 9, 2026 | 8,994 |
| Sep 8, 2026 | 7,806 |
| Sep 7, 2026 | 8,128 |
| Sep 6, 2026 | 8,602 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/821717323-root)<a href="https://payloaddirectory.dev/plugins/821717323-root"><img src="https://payloaddirectory.dev/badge/821717323-root.svg" alt="Payload Directory"></a>README
If you find this plugin useful, consider supporting its development through donations. Your contributions help improve security and stability!
TOTP is a widely used authentication mechanism that generates a unique, time-sensitive code based on a shared secret and the current time. This code is typically used as a second factor in authentication (2FA), adding an extra layer of security to protect against unauthorized access.
This plugin enhances security by wrapping the existing access controls under a TOTP verification process. Users must enter a valid TOTP code generated by an authenticator app (such as Google Authenticator, Authy, or Microsoft Authenticator) to gain access, reducing the risk of unauthorized logins even if credentials are compromised.
Install the plugin using any JavaScript package manager like pnpm, npm, or Yarn:
pnpm add payload-totp
In the plugins array of your Payload Config, call the plugin with options:
import { buildConfig } from 'payload'
import { payloadTotp } from 'payload-totp'
const config = buildConfig({
collections: [
{
slug: 'users',
auth: true,
fields: [],
},
],
plugins: [
payloadTotp({
collection: 'users',
// see below for a list of available options
}),
],
})
export default config
IMPORTANT: The plugin overrides all collections, therefore it should be the last plugin in the array, or at least not followed by plugins that add collections/globals. Furthermore, the plugin can break the default behavior for non–user-based access. Please read the Access Wrapper section.
Now, you need to modify the proxy.ts or create it if doesn't exist. The proxy will pass the pathname as a header. This is necessary because we don't have access to the pathname in the server-side component. Without this part, it will create an endless redirect loop.
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname
const response = NextResponse.next()
response.headers.append('x-pathname', pathname)
return response
}
Next.js 15: the proxy.ts convention was introduced in Next.js 16. On Next.js 15, use the same code in middleware.ts and name the exported function middleware instead.
collectionThe collection property specifies which collection with auth enabled should have TOTP protection. Currently, the plugin supports TOTP for a single collection at a time.
disabledAllows you to conditionally disable the plugin based on runtime conditions.
payloadTotp({
collection: 'users',
disabled: process.env.NODE_ENV === 'development',
})
When disabled, none of the plugin's behaviour is registered: your own access functions are left alone, the TOTP strategy, the admin provider, the setup and verify views and the endpoints are not added, and the account page shows no authenticator field.
The totpSecret field stays on the collection, and the totp-attempts
collection of the brute-force protection stays
registered, so toggling the option doesn't change the database schema and users
who had already enrolled keep their secret for whenever the plugin is enabled
again.
forceSetupBy default, the plugin does not force users to configure TOTP. The TOTP verification will only be prompted if the user has configured it. This option forces all users to configure their TOTP after login, enhancing security by ensuring 2FA is enabled for all accounts.
The access wrapper enforces it as well, not only the admin panel. Until a user has set TOTP up, every operation the access wrapper covers is refused, except reading their own document in the TOTP collection, which /api/<collection>/me needs; setting TOTP up goes through the plugin's own endpoint. This holds for REST, GraphQL and the Local API with overrideAccess: false, and for totpAccess applied by hand. API keys, and users of other auth collections (who can't set TOTP up), are not affected. A user counts as enrolled through the hasTotp field read with their document, so a custom auth strategy that builds the user without it is treated as not enrolled.
Access left unwrapped (disableAccessWrapper, custom.totp.disableAccessWrapper) and custom endpoints are not covered. Wrap them with totpAccess; on the TOTP collection's read, pass allowOwnDocumentDuringSetup so that me keeps working:
read: totpAccess(readAccess, { allowOwnDocumentDuringSetup: true }),
disableAccessWrapperThe disableAccessWrapper property disables the default access wrapper for all collections and globals. Read more about it.
totpThe totp property is used to configure the TOTP class from the otpauth package. You can customize the following options:
algorithm: The hash algorithm to use (e.g. 'SHA1', 'SHA256', 'SHA512')digits: Number of digits in the generated token (default: 6)issuer: The issuer name to display in authenticator appsperiod: Token validity period in seconds (default: 30)forceWhiteBackgroundOnQrCodeWhen enabled, the QR code shown on the setup page is rendered with a white background to improve readability when using a dark theme. Disabled by default.
By default, PayloadCMS has access set to ({user}) => Boolean(user). Since PayloadCMS naturally handles access for logged-in users, this plugin follows the same pattern.
The plugin will override the provided access function. This means that TOTP verifications will be called first, and if successful, it will then call the original function and return its result. This approach ensures compatibility with role-based access control and other custom access patterns.
Payload adds some collections of its own after all plugins have run. When Payload initializes, the plugin wraps the ones that hold data — jobs and the global their schedules are computed from, folders and query presets — and writes to document locks; reading locks stays open, because the dashboard needs it, and so do per-user preferences. Collections added by plugins listed after this one are not wrapped, which is one more reason to keep it last. The jobs.access.run function behind /api/payload-jobs/run and /api/payload-jobs/handle-schedules is not wrapped either; if you run jobs that way, combine your check with totpAccess:
jobs: {
access: {
run: async (args) =>
args.req.headers.get('authorization') === `Bearer ${process.env.CRON_SECRET}` ||
(await totpAccess()(args)) === true,
},
},
There are cases where collections or globals need to be available for non-logged-in users. To handle this, the plugin provides disableAccessWrapper globally or per global/collection. If you have many collections/globals that provide custom access or public access, you should use the plugin options disableAccessWrapper.
In case of changing the default access, you can deactivate per collection/global:
import type { CollectionConfig } from 'payload'
export const posts: CollectionConfig = {
slug: 'posts',
access: {
read: () => true,
},
fields: [],
custom: {
totp: {
disableAccessWrapper: {
read: true,
},
},
},
}
The disableAccessWrapper from custom has the same type as access in Collection/Global based on the context.
In case that you need a more complex access, for example based on a header or auth user, you can import totpAccess:
import type { CollectionConfig } from 'payload'
import { totpAccess } from 'payload-totp'
export const posts: CollectionConfig = {
slug: 'posts',
admin: {
useAsTitle: 'title',
},
access: {
read: (args) => {
return (
args.req.headers.get('authorization') === 'Bearer 123' ||
totpAccess(({ req: { user } }) => Boolean(user))(args)
)
},
},
fields: [],
custom: {
totp: {
disableAccessWrapper: {
read: true,
},
},
},
}
A code has a million possible values with the default six digits, and the verification accepts the codes for the previous and next 30-second step as well, so an unlimited number of tries would eventually land on one. Every code submitted to /verify-totp and /remove-totp is therefore counted: after maxLoginAttempts codes without a correct one, codes are refused for lockTime, exactly as passwords are after that many wrong ones. The forms show Payload's usual "too many failed login attempts" message, and the lock is copied to the user so that the password login answers the same. Another user can lift the lock with Force Unlock on the locked user's edit view. A correct code clears the count.
Nothing needs to be configured: maxLoginAttempts and lockTime are the collection's own, and maxLoginAttempts: 0 turns the lockout off for codes as it does for passwords.
Payload's default unlock access is any logged-in user. Users cannot lift their own lock on codes, but any other account can, so restrict who unlocks, as you would to protect the password lockout:
access: {
unlock: ({ req: { user } }) => user?.role === 'admin',
},
The count and the lock are kept in a collection that the plugin adds, totp-attempts, with one document per user who has submitted a code, under the user's ID. They are not on the user document because Payload's login, refresh and logout write back the whole user document they read when they started, which would let someone who knows the password put the count back. The collection is hidden from the admin panel, left out of GraphQL, and its access functions refuse every request; it stays registered when the plugin is disabled, so the schema does not change with the option. On Postgres and SQLite it is a new table, totp_attempts, so create and run a migration when you upgrade.
After logging in, navigate to your account settings where you'll find the "Authenticator app" field. If forceSetup is enabled, you'll be automatically redirected to the Setup TOTP page.
Click the "Setup" button to proceed to the Setup TOTP Page:
Scan the QR code or copy the secret into your preferred authenticator app, then enter the generated PIN code.
Upon successful verification, you'll be redirected back to your account page.
Now, when you log out and log back in, you'll be prompted to enter your TOTP PIN code:
After entering the correct PIN, you'll be redirected to the main dashboard page.
payload better auth
Integrate Better Auth into Payload CMS 3 with generated collections, API endpoints and admin login
3474v3
payload better auth
Better Auth database adapter and plugins that run authentication through Payload collections
9011.9k/wk77v3 v4
payload plugin masquerade
Let admins switch to another user and browse as them without a password, then switch back
8083/wk30v3
payload auth
Replace Payload's built-in auth with Better Auth for a shared admin and frontend session
7021.0k/wk349v3
payload keycloak
Keycloak SSO-only authentication for Payload 3 using OIDC with PKCE and permission-based access helpers
5511/wk0v3
payload auth pwless
Passwordless login for Payload with passkeys, magic links, OAuth and refresh-token sessions
4214/wk2v3