#!/usr/bin/env node /** * Screenshots of the panel's sign-in screens, for docs/dev/login-page/ (design review, before / after). * * node scripts/login-page-shots.mjs --prefix=after --base=http://localhost:3310 [--only=login,forgot] [--logo=keep|none] * * Needs the reference site running at --base with a seeded local database and the E2E_* test admin of * reference/.env (reference/README.md). Writes docs/dev/login-page/--.png: * login (light, dark, phone, Polish), login with a wrong password, forgot password, the 2FA code screen, * the 2FA setup screen (a throw-away editor, removed at the end). * --logo=none takes the logo out of Site settings for the run and puts it back (the "no logo" pictures); * --logo= uploads that picture, sets it as the logo for the run and puts the old value back. * Dark pictures set Payload's theme cookie; the development indicator of Next is hidden. * create-first-user needs a database without users: point --base at such a site and pass --only=first-user. */ import { createHmac } from 'node:crypto' import { mkdirSync, readFileSync } from 'node:fs' import { createRequire } from 'node:module' import path from 'node:path' import { fileURLToPath } from 'node:url' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const require = createRequire(path.join(root, 'reference/package.json')) const { chromium, request } = require('@playwright/test') try { process.loadEnvFile(path.join(root, 'reference/.env')) } catch { /* values come from the environment */ } const arg = (name, fallback) => process.argv.find((a) => a.startsWith(`--${name}=`))?.slice(name.length + 3) ?? fallback const base = arg('base', process.env.E2E_BASE_URL ?? 'http://localhost:3310') const prefix = arg('prefix', 'after') const only = arg('only')?.split(',') const logoMode = arg('logo', 'keep') const suffix = arg('suffix', logoMode === 'none' ? '-name' : logoMode === 'keep' ? '' : '-logo') const out = path.join(root, 'docs/dev/login-page') mkdirSync(out, { recursive: true }) const want = (name) => !only || only.includes(name) const admin = { email: process.env.E2E_ADMIN_EMAIL, password: process.env.E2E_ADMIN_PASSWORD, secret: process.env.E2E_TOTP_SECRET } function totp(secret, now = Date.now()) { const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567' let bits = '' for (const ch of secret.replace(/=+$/, '').toUpperCase()) {bits += alphabet.indexOf(ch).toString(2).padStart(5, '0')} const key = Buffer.from((bits.match(/.{8}/g) ?? []).map((b) => parseInt(b, 2))) const counter = Buffer.alloc(8) counter.writeBigUInt64BE(BigInt(Math.floor(now / 1000 / 30))) const hmac = createHmac('sha1', key).update(counter).digest() const offset = hmac[hmac.length - 1] & 0xf return ((hmac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000).toString().padStart(6, '0') } const DESKTOP = { height: 800, width: 1280 } const PHONE = { height: 844, width: 390 } const browser = await chromium.launch() async function shot(name, { colorScheme = 'light', locale = 'en-US', viewport = DESKTOP } = {}, steps) { const context = await browser.newContext({ baseURL: base, colorScheme, deviceScaleFactor: 1, locale, reducedMotion: 'reduce', viewport }) if (colorScheme === 'dark') {await context.addCookies([{ name: 'payload-theme', url: base, value: 'dark' }])} const page = await context.newPage() try { await steps(page) await page.addStyleTag({ content: 'nextjs-portal{display:none!important}' }) await page.waitForLoadState('networkidle').catch(() => undefined) await page.waitForTimeout(400) const file = path.join(out, `${prefix}-${name}${suffix}.png`) await page.screenshot({ fullPage: true, path: file }) console.log(`wrote ${path.relative(root, file)}`) } finally { await context.close() } } const signIn = async (page, who) => { await page.goto('/admin/login') await page.waitForLoadState('networkidle') await page.locator('#field-email').pressSequentially(who.email) await page.locator('#field-password').pressSequentially(who.password) await page.click('.form-submit button') } /** * ONE fully signed-in API session for the whole run, of the second test admin: a 2FA code counts against the * account's verify limit (5 in 10 minutes), and the first admin's screens are the ones photographed. */ let session = null async function adminApi() { if (session) {return session} const api = await request.newContext({ baseURL: base, extraHTTPHeaders: { origin: base } }) const login = await api.post('/api/users/login', { data: { email: admin.email.replace('@', '-2@'), password: admin.password } }) if (login.status() !== 200) {throw new Error(`test admin cannot sign in (${login.status()})`)} const verify = await (await api.post('/api/verify-totp', { data: { token: totp(admin.secret) } })).json() if (!verify.ok) {throw new Error('test admin: the 2FA code was refused (verify limit? run the reference site\'s e2e/create-test-admin.ts)')} session = api return api } let restoreLogo = null if (logoMode !== 'keep') { const api = await adminApi() const settings = await (await api.get('/api/globals/site-settings?depth=0')).json() let logo = null let uploaded = null if (logoMode !== 'none') { const file = path.resolve(logoMode) const res = await api.post('/api/media', { multipart: { _payload: JSON.stringify({ alt: 'Placeholder logo' }), file: { buffer: readFileSync(file), mimeType: 'image/png', name: path.basename(file) } }, }) uploaded = (await res.json())?.doc?.id if (!uploaded) {throw new Error(`the logo was not uploaded (${res.status()})`)} logo = uploaded } await api.post('/api/globals/site-settings', { data: { logo } }) restoreLogo = async () => { await api.post('/api/globals/site-settings', { data: { logo: settings.logo ?? null } }) if (uploaded) {await api.delete(`/api/media/${uploaded}`)} } } try { if (want('login')) { await shot('login-light', {}, (page) => page.goto('/admin/login')) await shot('login-dark', { colorScheme: 'dark' }, (page) => page.goto('/admin/login')) await shot('login-phone', { viewport: PHONE }, (page) => page.goto('/admin/login')) await shot('login-phone-dark', { colorScheme: 'dark', viewport: PHONE }, (page) => page.goto('/admin/login')) await shot('login-pl', { locale: 'pl-PL' }, (page) => page.goto('/admin/login')) } if (want('error')) { const wrong = async (page) => { await signIn(page, { email: 'nobody@example.com', password: 'wrong-password-1' }) await page.locator('.payload-toast-container li, .toast-error').first().waitFor({ timeout: 10_000 }).catch(() => undefined) } await shot('login-error-light', {}, wrong) await shot('login-error-phone', { viewport: PHONE }, wrong) } if (want('forgot')) { await shot('forgot-light', {}, (page) => page.goto('/admin/forgot')) await shot('forgot-dark', { colorScheme: 'dark' }, (page) => page.goto('/admin/forgot')) await shot('forgot-phone', { viewport: PHONE }, (page) => page.goto('/admin/forgot')) } if (want('verify') && admin.email) { const verify = async (page) => { await signIn(page, admin) await page.waitForURL(/verify-totp/, { timeout: 20_000 }) } await shot('2fa-code-light', {}, verify) await shot('2fa-code-dark', { colorScheme: 'dark' }, verify) await shot('2fa-code-phone', { viewport: PHONE }, verify) } if (want('setup') && admin.email) { const api = await adminApi() const editor = { email: `shots-editor-${Date.now()}@example.com`, password: `shots-${Date.now()}-Qm48xZr` } const created = await (await api.post('/api/users', { data: { ...editor, roles: ['editor'] } })).json() const id = created?.doc?.id try { const setup = async (page) => { await signIn(page, editor) await page.waitForURL(/setup-totp/, { timeout: 20_000 }) } await shot('2fa-setup-light', {}, setup) await shot('2fa-setup-dark', { colorScheme: 'dark' }, setup) await shot('2fa-setup-phone', { viewport: PHONE }, setup) } finally { if (id) {await api.delete(`/api/users/${id}`)} } } if (only?.includes('first-user')) { const first = async (page) => { await page.goto('/admin/create-first-user') await page.locator('#field-confirm-password').waitFor({ timeout: 20_000 }) } await shot('first-user-light', {}, first) await shot('first-user-dark', { colorScheme: 'dark' }, first) await shot('first-user-phone', { viewport: PHONE }, first) } } finally { await restoreLogo?.() await session?.dispose() await browser.close() }