Implement OTP-based email verification system
Add custom email verification using 6-digit OTP codes via Mailketing API: Database: - Create auth_otps table with 15-minute expiry - Add indexes and RLS policies for security - Add cleanup function for expired tokens - Insert default auth_email_verification template Edge Functions: - send-auth-otp: Generate OTP, store in DB, send via Mailketing - verify-auth-otp: Validate OTP, confirm email in Supabase Auth Frontend: - Add OTP input state to auth page - Implement send/verify OTP in useAuth hook - Add resend countdown timer (60 seconds) - Update auth flow: signup → OTP verification → login Features: - Instant email delivery (no queue/cron) - 6-digit OTP with 15-minute expiry - Resend OTP with cooldown - Admin-configurable email templates - Indonesian UI text 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
68
supabase/migrations/20250102000001_auth_otp.sql
Normal file
68
supabase/migrations/20250102000001_auth_otp.sql
Normal file
@@ -0,0 +1,68 @@
|
||||
-- ============================================================================
|
||||
-- Auth OTP System - Email Verification with One-Time Passwords
|
||||
-- ============================================================================
|
||||
|
||||
-- Create auth_otps table to store OTP codes for email verification
|
||||
CREATE TABLE IF NOT EXISTS auth_otps (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
email TEXT NOT NULL,
|
||||
otp_code TEXT NOT NULL,
|
||||
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
used_at TIMESTAMP WITH TIME ZONE,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create indexes for performance
|
||||
CREATE INDEX IF NOT EXISTS idx_auth_otps_user_otp ON auth_otps(user_id, otp_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_auth_otps_expires_at ON auth_otps(expires_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_auth_otps_email ON auth_otps(email);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE auth_otps ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- RLS Policies
|
||||
-- Service role has full access
|
||||
CREATE POLICY "Service role can do everything" ON auth_otps
|
||||
FOR ALL
|
||||
TO service_role
|
||||
USING (true)
|
||||
WITH CHECK (true);
|
||||
|
||||
-- Authenticated users can query their own OTPs
|
||||
CREATE POLICY "Users can view own OTPs" ON auth_otps
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid() = user_id);
|
||||
|
||||
-- Edge functions (via service role) can insert OTPs
|
||||
CREATE POLICY "Service role can insert OTPs" ON auth_otps
|
||||
FOR INSERT
|
||||
TO service_role
|
||||
WITH CHECK (true);
|
||||
|
||||
-- Edge functions (via service role) can update OTPs (mark as used)
|
||||
CREATE POLICY "Service role can update OTPs" ON auth_otps
|
||||
FOR UPDATE
|
||||
TO service_role
|
||||
USING (true)
|
||||
WITH CHECK (true);
|
||||
|
||||
-- Function to clean up expired OTPs
|
||||
CREATE OR REPLACE FUNCTION cleanup_expired_otps()
|
||||
RETURNS void AS $$
|
||||
BEGIN
|
||||
DELETE FROM auth_otps
|
||||
WHERE expires_at < NOW();
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Comment on table
|
||||
COMMENT ON TABLE auth_otps IS 'Stores OTP codes for email verification. Generated when users register, used to confirm email addresses.';
|
||||
|
||||
-- Comment on columns
|
||||
COMMENT ON COLUMN auth_otps.user_id IS 'Reference to auth.users.id';
|
||||
COMMENT ON COLUMN auth_otps.email IS 'Email address being verified (for redundancy)';
|
||||
COMMENT ON COLUMN auth_otps.otp_code IS '6-digit OTP code';
|
||||
COMMENT ON COLUMN auth_otps.expires_at IS 'OTP expiration time (typically 15 minutes)';
|
||||
COMMENT ON COLUMN auth_otps.used_at IS 'When OTP was used (NULL if unused)';
|
||||
162
supabase/migrations/20250102000002_auth_email_template.sql
Normal file
162
supabase/migrations/20250102000002_auth_email_template.sql
Normal file
@@ -0,0 +1,162 @@
|
||||
-- ============================================================================
|
||||
-- Auth Email Verification Template
|
||||
-- ============================================================================
|
||||
|
||||
-- Insert default auth email verification template
|
||||
INSERT INTO notification_templates (
|
||||
template_key,
|
||||
template_name,
|
||||
description,
|
||||
subject,
|
||||
html_content,
|
||||
is_active,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
'auth_email_verification',
|
||||
'Verifikasi Email - OTP',
|
||||
'Template untuk mengirim kode OTP verifikasi email saat pendaftaran',
|
||||
'Kode Verifikasi Email Anda - {platform_name}',
|
||||
'---
|
||||
<!DOCTYPE html>
|
||||
<html lang="id">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Verifikasi Email</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
||||
background-color: #f5f5f5;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.container {
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
background: white;
|
||||
border: 3px solid #000;
|
||||
box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
.header {
|
||||
background: #000;
|
||||
color: #fff;
|
||||
padding: 30px;
|
||||
text-align: center;
|
||||
}
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
font-weight: 700;
|
||||
}
|
||||
.content {
|
||||
padding: 30px;
|
||||
}
|
||||
.greeting {
|
||||
font-size: 18px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.message {
|
||||
margin-bottom: 30px;
|
||||
color: #333;
|
||||
}
|
||||
.otp-container {
|
||||
background: #f9f9f9;
|
||||
border: 2px dashed #000;
|
||||
padding: 30px;
|
||||
text-align: center;
|
||||
margin: 30px 0;
|
||||
}
|
||||
.otp-code {
|
||||
font-size: 36px;
|
||||
font-weight: 700;
|
||||
letter-spacing: 8px;
|
||||
color: #000;
|
||||
font-family: "Courier New", monospace;
|
||||
margin: 20px 0;
|
||||
}
|
||||
.expiry {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
margin-top: 10px;
|
||||
}
|
||||
.instructions {
|
||||
background: #fffbeb;
|
||||
border-left: 4px solid #f59e0b;
|
||||
padding: 15px;
|
||||
margin: 20px 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
.footer {
|
||||
background: #f9f9f9;
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
font-size: 12px;
|
||||
color: #666;
|
||||
border-top: 2px solid #000;
|
||||
}
|
||||
.button {
|
||||
display: inline-block;
|
||||
background: #000;
|
||||
color: #fff;
|
||||
padding: 12px 24px;
|
||||
text-decoration: none;
|
||||
font-weight: 600;
|
||||
margin-top: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1>🔐 Verifikasi Email</h1>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<p class="greeting">Halo {nama},</p>
|
||||
|
||||
<p class="message">
|
||||
Terima kasih telah mendaftar di <strong>{platform_name}</strong>!
|
||||
Gunakan kode OTP berikut untuk memverifikasi alamat email Anda:
|
||||
</p>
|
||||
|
||||
<div class="otp-container">
|
||||
<div class="otp-code">{otp_code}</div>
|
||||
<div class="expiry">⏰ Berlaku selama {expiry_minutes} menit</div>
|
||||
</div>
|
||||
|
||||
<div class="instructions">
|
||||
<strong>Cara menggunakan:</strong><br>
|
||||
1. Salin kode 6 digit di atas<br>
|
||||
2. Kembali ke halaman pendaftaran<br>
|
||||
3. Masukkan kode tersebut pada form verifikasi
|
||||
</div>
|
||||
|
||||
<p class="message" style="margin-top: 30px;">
|
||||
Jika Anda tidak merasa mendaftar di {platform_name},
|
||||
abaikan email ini dengan aman.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<p>Email ini dikirim ke {email}</p>
|
||||
<p>© {year} {platform_name}. Semua hak dilindungi.</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
---',
|
||||
true,
|
||||
NOW(),
|
||||
NOW()
|
||||
) ON CONFLICT (template_key) DO NOTHING;
|
||||
|
||||
-- Add comment
|
||||
COMMENT ON TABLE notification_templates IS 'Contains email templates for various notifications including auth emails';
|
||||
|
||||
-- Return success message
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE 'Auth email verification template inserted successfully';
|
||||
END $$;
|
||||
Reference in New Issue
Block a user