GeorgeHulpoi

payload totp

by GeorgeHulpoi
Authentication & SSO
v3
^3.88.0
MIT

Overview

Summary generated from the README

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.

Features

  • Setup page with QR code and secret for authenticator apps
  • Access wrapper around collections and globals, with per-collection opt-out
  • Optional forced setup for all users, enforced in REST, GraphQL and Local API
  • Brute-force lockout using the collection's maxLoginAttempts and lockTime
  • Configurable algorithm, digits, period and issuer via otpauth
  • Internationalized UI with dark and light theme support

Use cases

  • Protect admin panel logins with an authenticator app code in addition to a password
  • Require every editor to enroll in 2FA before accessing content through the API or admin
  • Limit guessing of one-time codes by locking accounts after repeated failures

From the author

Add an extra security layer to PayloadCMS using a Time-based One-time Password (TOTP).

Installation

npm i payload-totp

168

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

GitHub stars

168+9 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 2026168
Dec 4, 2026168
Dec 3, 2026168
Dec 2, 2026168
Dec 1, 2026168
Nov 30, 2026168
Nov 29, 2026168
Nov 28, 2026168
Nov 27, 2026168
Nov 26, 2026168
Nov 25, 2026168
Nov 24, 2026168
Nov 23, 2026168
Nov 22, 2026168
Nov 21, 2026168
Nov 20, 2026167
Nov 19, 2026167
Nov 18, 2026167
Nov 17, 2026167
Nov 16, 2026166
Nov 15, 2026166
Nov 14, 2026166
Nov 13, 2026166
Nov 12, 2026166
Nov 11, 2026165
Nov 10, 2026165
Nov 9, 2026164
Nov 8, 2026164
Nov 7, 2026164
Nov 6, 2026164
Nov 5, 2026164
Nov 4, 2026164
Nov 3, 2026164
Nov 2, 2026164
Nov 1, 2026163
Oct 31, 2026163
Oct 30, 2026163
Oct 29, 2026163
Oct 28, 2026163
Oct 27, 2026163
Oct 26, 2026163
Oct 25, 2026163
Oct 24, 2026163
Oct 23, 2026163
Oct 22, 2026163
Oct 21, 2026163
Oct 20, 2026163
Oct 19, 2026163
Oct 18, 2026163
Oct 17, 2026163
Oct 16, 2026163
Oct 15, 2026163
Oct 14, 2026163
Oct 13, 2026163
Oct 12, 2026163
Oct 11, 2026163
Oct 10, 2026163
Oct 9, 2026163
Oct 8, 2026163
Oct 7, 2026161
Oct 6, 2026161
Oct 5, 2026161
Oct 4, 2026161
Oct 3, 2026161
Oct 2, 2026161
Oct 1, 2026161
Sep 30, 2026161
Sep 29, 2026161
Sep 28, 2026161
Sep 27, 2026161
Sep 26, 2026161
Sep 25, 2026161
Sep 24, 2026160
Sep 23, 2026160
Sep 22, 2026160
Sep 21, 2026160
Sep 20, 2026160
Sep 19, 2026160
Sep 18, 2026160
Sep 17, 2026159
Sep 16, 2026159
Sep 15, 2026159
Sep 14, 2026159
Sep 13, 2026159
Sep 12, 2026159
Sep 11, 2026159
Sep 10, 2026159
Sep 9, 2026159
Sep 8, 2026159
Sep 7, 2026159
Sep 6, 2026159
npm weekly downloads

24.4K+15.8K since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202624,404
Dec 4, 202624,183
Dec 3, 202623,458
Dec 2, 202624,211
Dec 1, 202624,278
Nov 30, 202624,036
Nov 29, 202625,856
Nov 28, 202626,953
Nov 27, 202627,578
Nov 26, 202628,380
Nov 25, 202629,495
Nov 24, 202630,086
Nov 23, 202630,560
Nov 22, 202629,640
Nov 21, 202629,169
Nov 20, 202628,844
Nov 19, 202627,869
Nov 18, 202626,035
Nov 17, 202625,696
Nov 16, 202625,522
Nov 15, 202624,114
Nov 14, 202622,368
Nov 13, 202620,860
Nov 12, 202619,826
Nov 11, 202619,479
Nov 10, 202619,460
Nov 9, 202618,608
Nov 8, 202618,279
Nov 7, 202618,348
Nov 6, 202618,017
Nov 5, 202617,428
Nov 4, 202616,829
Nov 3, 202616,255
Nov 2, 202616,483
Nov 1, 202616,598
Oct 31, 202616,189
Oct 30, 202615,803
Oct 29, 202613,452
Oct 28, 202610,784
Oct 27, 202610,736
Oct 26, 202611,338
Oct 25, 202610,621
Oct 24, 202610,160
Oct 23, 20267,779
Oct 22, 202610,529
Oct 21, 202612,781
Oct 20, 202612,717
Oct 19, 202612,054
Oct 18, 202612,216
Oct 17, 202612,416
Oct 16, 202614,293
Oct 15, 202613,819
Oct 14, 202613,436
Oct 13, 202613,551
Oct 12, 202613,257
Oct 11, 202612,621
Oct 10, 202612,722
Oct 9, 202612,809
Oct 8, 202612,533
Oct 7, 202612,808
Oct 6, 202612,633
Oct 5, 202612,833
Oct 4, 202614,256
Oct 3, 202614,523
Oct 2, 202619,487
Oct 1, 202624,922
Sep 30, 202627,627
Sep 29, 202627,884
Sep 28, 202628,651
Sep 27, 202626,534
Sep 26, 202624,850
Sep 25, 202619,453
Sep 24, 202613,791
Sep 23, 202610,154
Sep 22, 20269,322
Sep 21, 20267,855
Sep 20, 202613,445
Sep 19, 202616,154
Sep 18, 202616,005
Sep 17, 202615,728
Sep 16, 202615,796
Sep 15, 202617,069
Sep 14, 202617,463
Sep 13, 202612,028
Sep 12, 20269,443
Sep 11, 20269,588
Sep 10, 20269,292
Sep 9, 20268,994
Sep 8, 20267,806
Sep 7, 20268,128
Sep 6, 20268,602

Each point is npm's rolling 7-day download count.

Health Score

84/100Excellent

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/821717323-root.svg)](https://payloaddirectory.dev/plugins/821717323-root)
HTML
<a href="https://payloaddirectory.dev/plugins/821717323-root"><img src="https://payloaddirectory.dev/badge/821717323-root.svg" alt="Payload Directory"></a>
Updated Oct 30, 2026Created Jun 29, 2024Published Oct 30, 2026

README

Payload TOTP (Time-based One-Time Password)

NPM Downloads banner

Sponsor Me Follow on LinkedIn Follow on X Read my blog

If you find this plugin useful, consider supporting its development through donations. Your contributions help improve security and stability!

What Does This Plugin Do?

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.

Core Features

  • Seamlessly integrates with existing access controls.
  • Full internationalization (i18n) support for all UI elements.
  • Built-in support for both dark and light themes.
  • Compatible with API key authentication.
  • Works with any authentication strategy supported by PayloadCMS.
  • Locks the account after too many wrong codes, with the collection's login lockout settings.

Installation

Install the plugin using any JavaScript package manager like pnpm, npm, or Yarn:

pnpm add payload-totp

Basic Usage

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.

Options

collection

The collection property specifies which collection with auth enabled should have TOTP protection. Currently, the plugin supports TOTP for a single collection at a time.

disabled

Allows 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.

forceSetup

By 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 }),

disableAccessWrapper

The disableAccessWrapper property disables the default access wrapper for all collections and globals. Read more about it.

totp

The 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 apps
  • period: Token validity period in seconds (default: 30)

forceWhiteBackgroundOnQrCode

When 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.

Access Wrapper

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,
			},
		},
	},
}

Brute-force Protection

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.

Dashboard Walkthrough

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.

show_auth_field

Click the "Setup" button to proceed to the Setup TOTP Page:

setup_page

Scan the QR code or copy the secret into your preferred authenticator app, then enter the generated PIN code.

setup_video

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:

login_video

After entering the correct PIN, you'll be redirected to the main dashboard page.

License

MIT

Similar plugins