65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
import { getRedisClient } from '@/lib/redis';
|
|
|
|
// This route just views Redis email credentials without making any changes
|
|
export async function GET(request: Request) {
|
|
try {
|
|
// Authenticate user
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const redis = getRedisClient();
|
|
|
|
// Get all email credential keys
|
|
const keys = await redis.keys('email:credentials:*');
|
|
console.log(`Found ${keys.length} credential records in Redis`);
|
|
|
|
const credentials = [];
|
|
|
|
// Process each key
|
|
for (const key of keys) {
|
|
try {
|
|
// Extract user ID from key
|
|
const userId = key.split(':')[2];
|
|
|
|
// Get credentials from Redis
|
|
const credStr = await redis.get(key);
|
|
if (!credStr) continue;
|
|
|
|
// Parse credentials
|
|
const creds = JSON.parse(credStr);
|
|
|
|
// Add to results (remove sensitive data)
|
|
credentials.push({
|
|
userId,
|
|
email: creds.email,
|
|
host: creds.host,
|
|
port: creds.port,
|
|
hasPassword: !!creds.encryptedPassword,
|
|
// Include other non-sensitive fields
|
|
smtp_host: creds.smtp_host,
|
|
smtp_port: creds.smtp_port,
|
|
display_name: creds.display_name,
|
|
color: creds.color
|
|
});
|
|
} catch (error) {
|
|
console.error(`Error processing ${key}:`, error);
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({
|
|
count: credentials.length,
|
|
credentials
|
|
});
|
|
} catch (error) {
|
|
console.error('Error viewing Redis credentials:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to view credentials', details: error instanceof Error ? error.message : String(error) },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|