UNPKG

better-auth-backup-codes

Version:

Backup codes plugin for Better Auth - Recovery codes for two-factor authentication

82 lines (68 loc) • 2.65 kB
# better-auth-backup-codes Backup codes plugin for Better Auth - Recovery codes for two-factor authentication ## Status āš ļø **Work in Progress** - This package is under active development and not yet ready for production use. ## Features (Planned) - šŸ”‘ **Recovery Codes** - Generate secure backup codes for account recovery - šŸ” **Cryptographically Secure** - Use CSPRNG for code generation - šŸ“Š **Usage Tracking** - Track which codes have been used - šŸ”„ **Regeneration** - Allow users to regenerate codes when needed - šŸ“„ **Download Options** - Let users download codes as PDF or text - šŸ–Øļø **Print-friendly** - Formatted for easy printing and storage - āš ļø **Low Code Warnings** - Alert users when running low on codes - šŸ”’ **Secure Storage** - Hashed storage with proper salting - šŸ“± **2FA Integration** - Seamless integration with two-factor auth ## Installation ```bash npm install better-auth-backup-codes ``` ## Usage (Coming Soon) ```typescript import { betterAuth } from "better-auth"; import { backupCodes } from "better-auth-backup-codes"; const auth = betterAuth({ plugins: [ backupCodes({ // Code generation settings codes: { count: 10, // Number of codes to generate length: 8, // Length of each code format: "XXXX-XXXX", // Code format (X = alphanumeric) charset: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", }, // Security settings security: { hashCodes: true, // Store hashed instead of plain text oneTimeUse: true, // Each code can only be used once requireCurrentPassword: true, // Require password to view codes notifyOnUse: true, // Notify user when backup code is used }, // Warnings and limits warnings: { lowCodeThreshold: 3, // Warn when only 3 codes remain forceRegenerate: 0, // Force regeneration when 0 codes remain }, // Events onCodeUsed: async (user, code) => { // Send notification email console.log(`Backup code used for user ${user.id}`); }, onCodesRegenerated: async (user) => { // Log regeneration event }, onLowCodes: async (user, remaining) => { // Alert user about low codes }, }), ], }); ``` ## Security Best Practices - Store backup codes securely (hashed with salt) - Require current authentication to view/regenerate codes - Notify users immediately when a backup code is used - Encourage users to store codes in a safe place - Consider requiring backup codes for high-privilege accounts - Implement rate limiting on backup code attempts ## License MIT