cc351158f8
* feat: invoicing & account-security polish bundle Five independent improvements bundled to ship together: - BankID/password lockout fix: BankID-only users could enroll MFA and brick themselves (Supabase requires AAL2 to change password or unenroll MFA, and AAL2 needs a password sign-in). New app_metadata.has_password flag tracks this; middleware gates /mfa/enroll behind it, /account/set- password is the unlock path, SecuritySettings shows a banner, and /api/account/password is the single write path that flips the flag. Backfill script for existing users. - Swish invoice payment method: company_settings.swish + invoice_show_swish columns, validation in lib/api/schemas.ts (accepts 123XXXXXXX företag or 07XXXXXXXX mobile, strips whitespace/hyphens), rendered on invoice PDFs. - Send-reminders kill switch: per-company company_settings.send_invoice_ reminders toggle in PdfPrintSettings/Automatisering. Reminder processor also tightened: positive status allowlist (sent + overdue) so terminal statuses can never match; skip when customer already responded via reminder link; race-window re-check before send. - First-invoice logo prompt: one-shot dialog when creating the first invoice without a logo (issue #520). Self-limits via head-only count. - SIE export opening-balance fallback: route IB through getOpeningBalances so the compute_prior_opening_balances RPC supplies #IB after multi-year imports where opening_balance_entry_id is intentionally NULL. Previously #IB silently went to zero and #UB collapsed to current-period movements. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(account-polish): address PR review feedback - BankID-link path (extensions/general/tic/index.ts): read-merge-write app_metadata instead of passing { bankid_linked: true } alone. updateUserById REPLACES app_metadata wholesale, so the previous code would have wiped has_password for any user who later linked BankID, causing the set-password banner to (incorrectly) reappear and blocking the standard MFA enrollment button. The comment is now corrected. - Middleware (lib/supabase/middleware.ts): thread inner returnTo through the /mfa/enroll → /account/set-password redirect so the user lands on their original destination after the full chain completes, not on /. - safeReturnTo helper (lib/auth/safe-return-to.ts): replace the starts-with-/-but-not-// guard on mfa/enroll and set-password pages. The previous guard let /\evil.com and /@evil.com through. The new helper parses against a synthetic base origin and verifies it matches. - set-password page (app/(auth)/account/set-password/page.tsx): remove CLAUDE.md design system violations — bg-gradient-to-b on page bg, inline shadow-md style on the card, space-y-5, font-medium on the h1, rounded-xl on the card. Flat surface, hairline border, font-display h1 per the design tokens. - Swish dedup (lib/payments/swish.ts): extract normaliseSwish() and isValidSwish() helpers and use them in lib/api/schemas.ts, components/settings/BankDetailsForm.tsx, and the invoicing settings page. Single source of truth for the regex. - Password route (app/api/account/password/route.ts): emit a structured success log so the audit pipeline can detect password-set events, not just failures. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
106 lines
2.8 KiB
TypeScript
106 lines
2.8 KiB
TypeScript
#!/usr/bin/env npx tsx
|
|
/**
|
|
* Backfill auth.users.app_metadata.has_password for the BankID-MFA lockout fix.
|
|
*
|
|
* - BankID-linked users (app_metadata.bankid_linked === true) with the flag
|
|
* unset → set has_password = false. Banner in SecuritySettings will then
|
|
* guide them through /account/set-password before MFA enroll is unlocked.
|
|
*
|
|
* - All other users with the flag unset (legacy email/password signups) →
|
|
* set has_password = true. They have a real password.
|
|
*
|
|
* - Anyone with the flag already set is left alone — fully idempotent.
|
|
*
|
|
* Usage:
|
|
* npx tsx scripts/backfill-has-password.ts # apply
|
|
* npx tsx scripts/backfill-has-password.ts --dry-run # report only
|
|
*/
|
|
|
|
import { config } from 'dotenv'
|
|
config({ path: '.env.local' })
|
|
import { createClient } from '@supabase/supabase-js'
|
|
|
|
const DRY_RUN = process.argv.includes('--dry-run')
|
|
|
|
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
|
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
|
|
|
|
if (!supabaseUrl || !serviceRoleKey) {
|
|
console.error(
|
|
'Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local',
|
|
)
|
|
process.exit(1)
|
|
}
|
|
|
|
const supabase = createClient(supabaseUrl, serviceRoleKey)
|
|
|
|
async function main() {
|
|
let page = 1
|
|
const perPage = 200
|
|
let totalScanned = 0
|
|
let setFalse = 0
|
|
let setTrue = 0
|
|
let skipped = 0
|
|
|
|
for (;;) {
|
|
const { data, error } = await supabase.auth.admin.listUsers({
|
|
page,
|
|
perPage,
|
|
})
|
|
if (error) {
|
|
console.error('listUsers failed', error)
|
|
process.exit(1)
|
|
}
|
|
if (!data.users || data.users.length === 0) break
|
|
|
|
for (const user of data.users) {
|
|
totalScanned++
|
|
const meta = (user.app_metadata ?? {}) as Record<string, unknown>
|
|
const flagAlreadySet =
|
|
meta.has_password === true || meta.has_password === false
|
|
|
|
if (flagAlreadySet) {
|
|
skipped++
|
|
continue
|
|
}
|
|
|
|
const isBankIdLinked = meta.bankid_linked === true
|
|
const nextValue = isBankIdLinked ? false : true
|
|
|
|
if (DRY_RUN) {
|
|
if (nextValue) setTrue++
|
|
else setFalse++
|
|
continue
|
|
}
|
|
|
|
const merged = { ...meta, has_password: nextValue }
|
|
const { error: updateError } = await supabase.auth.admin.updateUserById(
|
|
user.id,
|
|
{ app_metadata: merged },
|
|
)
|
|
if (updateError) {
|
|
console.error(`failed to update user ${user.id}`, updateError)
|
|
continue
|
|
}
|
|
if (nextValue) setTrue++
|
|
else setFalse++
|
|
}
|
|
|
|
if (data.users.length < perPage) break
|
|
page++
|
|
}
|
|
|
|
console.log(JSON.stringify({
|
|
mode: DRY_RUN ? 'dry-run' : 'apply',
|
|
scanned: totalScanned,
|
|
set_true: setTrue,
|
|
set_false: setFalse,
|
|
skipped_already_set: skipped,
|
|
}, null, 2))
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|