-- Migration: Add Two-Factor Authentication Support -- Description: Adds 2FA columns to users table and creates table for email OTP codes -- Date: 2025-10-17 -- Add 2FA columns to users table ALTER TABLE `users` ADD COLUMN `two_factor_enabled` VARCHAR(1) NOT NULL DEFAULT 'F' COMMENT 'T=enabled, F=disabled' AFTER `role`, ADD COLUMN `two_factor_method` VARCHAR(20) NULL DEFAULT NULL COMMENT 'email or authenticator' AFTER `two_factor_enabled`, ADD COLUMN `two_factor_secret` VARCHAR(255) NULL DEFAULT NULL COMMENT 'TOTP secret for Google Authenticator' AFTER `two_factor_method`; -- Create table for email OTP codes CREATE TABLE `user_otp_codes` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` INT(11) UNSIGNED NOT NULL, `otp_code` VARCHAR(6) NOT NULL, `expires_at` TIMESTAMP NOT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `used` VARCHAR(1) NOT NULL DEFAULT 'F' COMMENT 'T=used, F=not used', PRIMARY KEY (`id`), KEY `idx_user_id` (`user_id`), KEY `idx_expires_at` (`expires_at`), CONSTRAINT `fk_otp_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- Create index for faster lookups CREATE INDEX `idx_users_two_factor` ON `users` (`two_factor_enabled`, `two_factor_method`);