Fix send-auth-otp: Remove notification_logs references

- Remove notification_logs table references (table doesn't exist)
- This was causing the function to crash after sending email
- Now the function should complete successfully
- Added better email payload logging
- Keep .env file for local development
This commit is contained in:
dwindown
2026-01-02 15:07:41 +07:00
parent fa1adcf291
commit 08e56a22d8
8 changed files with 421 additions and 31 deletions

8
.env Normal file
View File

@@ -0,0 +1,8 @@
SITE_URL=https://with.dwindi.com/
VITE_APP_ENV=production
VITE_GOOGLE_CLIENT_ID=650232746742-nup9nrp27001n0c6a3vqlc156g4tqfqa.apps.googleusercontent.com
VITE_PAKASIR_API_KEY=iP13osgh7lAzWWIPsj7TbW5M3iGEAQMo
VITE_PAKASIR_PROJECT_SLUG=withdwindi
VITE_SUPABASE_ANON_KEY=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc2NjAzNzEyMCwiZXhwIjo0OTIxNzEwNzIwLCJyb2xlIjoiYW5vbiJ9.Sa-eECy9dgBUQy3O4X5X-3tDPmF01J5zeT-Qtb-koYc
VITE_SUPABASE_EDGE_URL=https://lovable.backoffice.biz.id/functions/v1
VITE_SUPABASE_URL=https://lovable.backoffice.biz.id/

17
check-template.sql Normal file
View File

@@ -0,0 +1,17 @@
-- Check if the email template exists and has content
SELECT
key,
name,
is_active,
email_subject,
LENGTH(email_body_html) as html_length,
SUBSTRING(email_body_html, 1, 500) as html_preview,
CASE
WHEN email_body_html IS NULL THEN 'NULL - empty template'
WHEN LENGTH(email_body_html) < 100 THEN 'TOO SHORT - template incomplete'
WHEN email_body_html LIKE '%<html>%' THEN 'Has HTML tag'
WHEN email_body_html LIKE '%---%' THEN 'Has YAML delimiters'
ELSE 'Unknown format'
END as template_status
FROM notification_templates
WHERE key = 'auth_email_verification';

14
check_email_logs.sql Normal file
View File

@@ -0,0 +1,14 @@
-- Check recent notification logs for auth_email_verification
SELECT
id,
user_id,
email,
notification_type,
status,
provider,
error_message,
created_at
FROM notification_logs
WHERE notification_type = 'auth_email_verification'
ORDER BY created_at DESC
LIMIT 5;

30
cleanup-user.sql Normal file
View File

@@ -0,0 +1,30 @@
-- ============================================================================
-- Clean Up User from Supabase Auth Completely
-- ============================================================================
-- NOTE: You CANNOT just DELETE from auth.users
-- Supabase keeps deleted users in a recycle bin
-- To completely remove a user, you need to use Supabase Auth Admin API
-- OR use a cascade delete from a linked table
-- Option 1: Delete via cascade (if you have foreign keys)
-- This works because auth_otps has ON DELETE CASCADE
DELETE FROM auth.users WHERE email = 'your@email.com';
-- Option 2: Check if user still exists in recycle bin
SELECT id, email, deleted_at
FROM auth.users
WHERE email = 'your@email.com';
-- If you see deleted_at IS NOT NULL, the user is in recycle bin
-- To permanently delete from recycle bin, you need to:
-- 1. Go to Supabase Dashboard → Authentication → Users
-- 2. Find the user
-- 3. Click "Permanently delete"
-- OR use the Auth Admin API from an edge function:
/*
const { data, error } = await supabase.auth.admin.deleteUser(userId);
*/

91
debug-email.sh Executable file
View File

@@ -0,0 +1,91 @@
#!/bin/bash
# Test script to debug email sending issue
# Run this after registering a user
echo "🔍 OTP Email Debug Script"
echo "==========================="
echo ""
SUPABASE_URL="https://lovable.backoffice.biz.id"
SERVICE_KEY="YOUR_SERVICE_ROLE_KEY_HERE" # Replace with actual service role key
echo "1. Checking recent OTP records..."
echo "Run this in Supabase SQL Editor:"
echo ""
echo "SELECT"
echo " id,"
echo " user_id,"
echo " email,"
echo " otp_code,"
echo " expires_at,"
echo " used_at,"
echo " created_at"
echo "FROM auth_otps"
echo "ORDER BY created_at DESC"
echo "LIMIT 1;"
echo ""
echo "2. Checking notification logs..."
echo "Run this in Supabase SQL Editor:"
echo ""
echo "SELECT"
echo " id,"
echo " user_id,"
echo " email,"
echo " notification_type,"
echo " status,"
echo " provider,"
echo " error_message,"
echo " created_at"
echo "FROM notification_logs"
echo "WHERE notification_type = 'auth_email_verification'"
echo "ORDER BY created_at DESC"
echo "LIMIT 5;"
echo ""
echo "3. Checking notification settings..."
echo "Run this in Supabase SQL Editor:"
echo ""
echo "SELECT"
echo " platform_name,"
echo " from_name,"
echo " from_email,"
echo " api_token,"
echo " mailketing_api_token"
echo "FROM notification_settings"
echo "LIMIT 1;"
echo ""
echo "4. Checking email template..."
echo "Run this in Supabase SQL Editor:"
echo ""
echo "SELECT"
echo " key,"
echo " name,"
echo " is_active,"
echo " email_subject,"
echo " LEFT(email_body_html, 200) as email_preview"
echo "FROM notification_templates"
echo "WHERE key = 'auth_email_verification';"
echo ""
echo "5. Testing email sending manually..."
echo "Replace USER_ID and EMAIL with actual values from step 1, then run:"
echo ""
echo "curl -X POST ${SUPABASE_URL}/functions/v1/send-auth-otp \\"
echo " -H \"Authorization: Bearer ${SERVICE_KEY}\" \\"
echo " -H \"Content-Type: application/json\" \\"
echo " -d '{"
echo " \"user_id\": \"USER_UUID\","
echo " \"email\": \"your@email.com\""
echo " }'"
echo ""
echo "6. Common issues to check:"
echo " ✓ from_email is not 'noreply@example.com' (set real domain)"
echo " ✓ api_token or mailketing_api_token is set"
echo " ✓ Email template is_active = true"
echo " ✓ Mailketing API is accessible from Supabase server"
echo " ✓ Check notification_logs.error_message for specific error"
echo ""

View File

@@ -138,7 +138,6 @@ serve(async (req: Request) => {
hasFromName: !!settings.from_name, hasFromName: !!settings.from_name,
hasFromEmail: !!settings.from_email, hasFromEmail: !!settings.from_email,
platformName: settings.platform_name, platformName: settings.platform_name,
allSettings: JSON.stringify(settings)
}); });
// Use api_token (not mailketing_api_token) // Use api_token (not mailketing_api_token)
@@ -148,6 +147,16 @@ serve(async (req: Request) => {
throw new Error('API token not found in notification_settings'); throw new Error('API token not found in notification_settings');
} }
// Log email details (truncate HTML body for readability)
console.log('Email payload:', {
to: email,
from_name: settings.from_name || settings.platform_name || 'Admin',
from_email: settings.from_email || 'noreply@example.com',
subject: subject,
html_body_length: htmlBody.length,
html_body_preview: htmlBody.substring(0, 200),
});
const emailResponse = await fetch(`${supabaseUrl}/functions/v1/send-email-v2`, { const emailResponse = await fetch(`${supabaseUrl}/functions/v1/send-email-v2`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -173,17 +182,7 @@ serve(async (req: Request) => {
const emailResult = await emailResponse.json(); const emailResult = await emailResponse.json();
console.log('Email sent successfully:', emailResult); console.log('Email sent successfully:', emailResult);
// Log notification // Note: notification_logs table doesn't exist, skipping logging
await supabase
.from('notification_logs')
.insert({
user_id,
email: email,
notification_type: 'auth_email_verification',
status: 'sent',
provider: 'mailketing',
error_message: null,
});
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
@@ -196,25 +195,7 @@ serve(async (req: Request) => {
} catch (error: any) { } catch (error: any) {
console.error("Error sending OTP:", error); console.error("Error sending OTP:", error);
// Try to log error notification // Note: notification_logs table doesn't exist, skipping error logging
try {
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
const supabase = createClient(supabaseUrl, supabaseServiceKey);
await supabase
.from('notification_logs')
.insert({
user_id: null,
email: null,
notification_type: 'auth_email_verification',
status: 'failed',
provider: 'mailketing',
error_message: error.message || 'Unknown error',
});
} catch (logError) {
console.error('Failed to log error:', logError);
}
return new Response( return new Response(
JSON.stringify({ JSON.stringify({

View File

@@ -0,0 +1,145 @@
-- ============================================================================
-- Fix Auth Email Verification Template - Remove YAML delimiters
-- ============================================================================
-- Update the auth_email_verification template with proper HTML
UPDATE notification_templates
SET
email_subject = 'Kode Verifikasi Email Anda - {platform_name}',
email_body_html = '<!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;
}
</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>'
WHERE key = 'auth_email_verification';
-- Verify the update
SELECT
key,
name,
is_active,
email_subject,
LENGTH(email_body_html) as html_length,
SUBSTRING(email_body_html, 1, 100) as html_start,
CASE
WHEN email_body_html LIKE '%<!DOCTYPE%' THEN '✓ Has DOCTYPE'
WHEN email_body_html LIKE '%<html>%' THEN '✓ Has HTML tag'
WHEN email_body_html LIKE '%---%' THEN '✗ Still has YAML delimiters'
ELSE 'Unknown format'
END as template_status
FROM notification_templates
WHERE key = 'auth_email_verification';

104
test-otp-flow.sh Executable file
View File

@@ -0,0 +1,104 @@
#!/bin/bash
# Test OTP Email Flow
# This script tests the complete OTP email verification flow
echo "🔐 Testing OTP Email Verification Flow"
echo "========================================"
echo ""
# Configuration
SUPABASE_URL="https://lovable.backoffice.biz.id"
# You need to get your service role key from Coolify/Docker
SERVICE_KEY="YOUR_SERVICE_ROLE_KEY_HERE"
echo "⚠️ IMPORTANT: Replace YOUR_SERVICE_ROLE_KEY_HERE with your actual service role key"
echo " You can find it in Coolify → Supabase service → Environment variables"
echo ""
# Step 1: Register a test user (using curl)
echo "Step 1: Creating test user..."
echo "Run this in Supabase SQL Editor first:"
echo ""
echo "-- Clean up any existing test user"
echo "DELETE FROM auth_otps WHERE email = 'test@example.com';"
echo "DELETE FROM auth.users WHERE email = 'test@example.com';"
echo ""
# Step 2: Test the send-auth-otp edge function
echo "Step 2: Testing send-auth-otp edge function..."
echo ""
echo "Replace USER_ID with the actual UUID from step 1, then run:"
echo ""
echo "curl -X POST ${SUPABASE_URL}/functions/v1/send-auth-otp \\"
echo " -H \"Authorization: Bearer \${SERVICE_KEY}\" \\"
echo " -H \"Content-Type: application/json\" \\"
echo " -d '{"
echo " \"user_id\": \"USER_UUID_HERE\","
echo " \"email\": \"test@example.com\""
echo " }'"
echo ""
# Step 3: Check if OTP was created
echo "Step 3: Check if OTP was created in database..."
echo "Run this in Supabase SQL Editor:"
echo ""
echo "SELECT"
echo " id,"
echo " user_id,"
echo " email,"
echo " otp_code,"
echo " expires_at,"
echo " used_at,"
echo " created_at"
echo "FROM auth_otps"
echo "WHERE email = 'test@example.com'"
echo "ORDER BY created_at DESC"
echo "LIMIT 1;"
echo ""
# Step 4: Check notification settings
echo "Step 4: Check notification settings..."
echo "Run this in Supabase SQL Editor:"
echo ""
echo "SELECT"
echo " id,"
echo " platform_name,"
echo " from_name,"
echo " from_email,"
echo " api_token,"
echo " mailketing_api_token"
echo "FROM notification_settings"
echo "LIMIT 1;"
echo ""
# Step 5: Check email template
echo "Step 5: Check email template..."
echo "Run this in Supabase SQL Editor:"
echo ""
echo "SELECT"
echo " key,"
echo " name,"
echo " is_active,"
echo " email_subject,"
echo " LENGTH(email_body_html) as html_length"
echo "FROM notification_templates"
echo "WHERE key = 'auth_email_verification';"
echo ""
echo "========================================"
echo "Common Issues:"
echo "========================================"
echo ""
echo "1. If curl returns 404: Edge function not deployed"
echo " Solution: Deploy the function from your server"
echo ""
echo "2. If curl returns 401: Invalid service role key"
echo " Solution: Check SERVICE_KEY is correct"
echo ""
echo "3. If curl returns 500: Check edge function logs"
echo " Solution: SSH into server and check Docker logs"
echo ""
echo "4. If OTP created but no email: Mailketing API issue"
echo " Solution: Check api_token in notification_settings"
echo ""