// Test script for Supabase authentication
// Make sure to set up your environment variables first
const API_BASE_URL = 'http://localhost:3000/api/mcp';
// Replace with a valid API key from your Supabase users table
const TEST_API_KEY = 'your_test_api_key_here';
async function testAuthentication() {
console.log('Testing Supabase authentication...\n');
// Test 1: Valid API key
try {
const response = await fetch(`${API_BASE_URL}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${TEST_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
method: 'tools/call',
params: {
name: 'get_user_info',
arguments: {}
}
})
});
const result = await response.json();
console.log('✅ Authentication successful:', result);
} catch (error) {
console.log('❌ Authentication failed:', error.message);
}
// Test 2: Invalid API key
try {
const response = await fetch(`${API_BASE_URL}`, {
method: 'POST',
headers: {
'Authorization': 'Bearer invalid_api_key',
'Content-Type': 'application/json',
},
body: JSON.stringify({
method: 'tools/call',
params: {
name: 'get_user_info',
arguments: {}
}
})
});
const result = await response.json();
console.log('✅ Invalid API key correctly rejected:', result);
} catch (error) {
console.log('❌ Unexpected error:', error.message);
}
// Test 3: Missing API key
try {
const response = await fetch(`${API_BASE_URL}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
method: 'tools/call',
params: {
name: 'get_user_info',
arguments: {}
}
})
});
const result = await response.json();
console.log('✅ Missing API key correctly rejected:', result);
} catch (error) {
console.log('❌ Unexpected error:', error.message);
}
}
// Instructions for setup
console.log(`
🚀 Supabase Authentication Test Script
Before running this script:
1. Set up your Supabase project and add environment variables:
- NEXT_PUBLIC_SUPABASE_URL
- SUPABASE_SERVICE_ROLE_KEY
2. Create the users table in Supabase:
CREATE TABLE users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
api_key TEXT UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
3. Insert a test user:
INSERT INTO users (email, api_key, is_admin)
VALUES ('test@example.com', 'your_test_api_key_here', false);
4. Update the TEST_API_KEY variable in this script with your actual API key
5. Start your Next.js development server: npm run dev
6. Run this script: node test-supabase-auth.js
`);
// Uncomment the line below to run the test
// testAuthentication();