courrier redis login
This commit is contained in:
parent
4899bd093b
commit
42746fe4f9
@ -3,6 +3,9 @@ import { ImapFlow } from 'imapflow';
|
|||||||
import { getServerSession } from 'next-auth';
|
import { getServerSession } from 'next-auth';
|
||||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||||
import { prisma } from '@/lib/prisma';
|
import { prisma } from '@/lib/prisma';
|
||||||
|
import { saveUserEmailCredentials, testEmailConnection } from '@/lib/services/email-service';
|
||||||
|
import { prefetchUserEmailData } from '@/lib/services/prefetch-service';
|
||||||
|
import { invalidateUserEmailCache, getCachedEmailCredentials } from '@/lib/redis';
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
@ -42,71 +45,39 @@ export async function POST(request: Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test IMAP connection
|
// Test connection before saving
|
||||||
const client = new ImapFlow({
|
const connectionSuccess = await testEmailConnection({
|
||||||
host: host,
|
email,
|
||||||
port: parseInt(port),
|
password,
|
||||||
secure: true,
|
host,
|
||||||
auth: {
|
port: parseInt(port)
|
||||||
user: email,
|
|
||||||
pass: password,
|
|
||||||
},
|
|
||||||
logger: false,
|
|
||||||
emitLogs: false,
|
|
||||||
tls: {
|
|
||||||
rejectUnauthorized: false // Allow self-signed certificates
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
if (!connectionSuccess) {
|
||||||
await client.connect();
|
|
||||||
await client.mailboxOpen('INBOX');
|
|
||||||
|
|
||||||
// Store or update credentials in database
|
|
||||||
await prisma.mailCredentials.upsert({
|
|
||||||
where: {
|
|
||||||
userId: session.user.id
|
|
||||||
},
|
|
||||||
update: {
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
host,
|
|
||||||
port: parseInt(port)
|
|
||||||
},
|
|
||||||
create: {
|
|
||||||
userId: session.user.id,
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
host,
|
|
||||||
port: parseInt(port)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof Error) {
|
|
||||||
if (error.message.includes('Invalid login')) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: 'Invalid login or password' },
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: `IMAP connection error: ${error.message}` },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to connect to email server' },
|
{ error: 'Failed to connect to email server. Please check your credentials.' },
|
||||||
{ status: 500 }
|
{ status: 401 }
|
||||||
);
|
);
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
await client.logout();
|
|
||||||
} catch (e) {
|
|
||||||
console.error('Error during logout:', e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate all cached data for this user
|
||||||
|
await invalidateUserEmailCache(session.user.id);
|
||||||
|
|
||||||
|
// Save credentials using the service that handles both database and Redis
|
||||||
|
await saveUserEmailCredentials(session.user.id, {
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
host,
|
||||||
|
port: parseInt(port)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start prefetching email data in the background
|
||||||
|
prefetchUserEmailData(session.user.id).catch(err => {
|
||||||
|
console.error('Background prefetch error:', err);
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in login handler:', error);
|
console.error('Error in login handler:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@ -126,16 +97,26 @@ export async function GET() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const credentials = await prisma.mailCredentials.findUnique({
|
// First try to get from Redis cache
|
||||||
where: {
|
let credentials = await getCachedEmailCredentials(session.user.id);
|
||||||
userId: session.user.id
|
|
||||||
},
|
// If not in cache, get from database
|
||||||
select: {
|
if (!credentials) {
|
||||||
email: true,
|
credentials = await prisma.mailCredentials.findUnique({
|
||||||
host: true,
|
where: {
|
||||||
port: true
|
userId: session.user.id
|
||||||
}
|
},
|
||||||
});
|
select: {
|
||||||
|
email: true,
|
||||||
|
host: true,
|
||||||
|
port: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Remove password from response
|
||||||
|
const { password, ...safeCredentials } = credentials;
|
||||||
|
credentials = safeCredentials;
|
||||||
|
}
|
||||||
|
|
||||||
if (!credentials) {
|
if (!credentials) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
23
lib/imap.ts
23
lib/imap.ts
@ -2,6 +2,7 @@ import { ImapFlow } from 'imapflow';
|
|||||||
import { getServerSession } from 'next-auth';
|
import { getServerSession } from 'next-auth';
|
||||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||||
import { prisma } from '@/lib/prisma';
|
import { prisma } from '@/lib/prisma';
|
||||||
|
import { getCachedEmailCredentials } from '@/lib/redis';
|
||||||
|
|
||||||
export async function getImapClient() {
|
export async function getImapClient() {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
@ -9,16 +10,28 @@ export async function getImapClient() {
|
|||||||
throw new Error('No authenticated user');
|
throw new Error('No authenticated user');
|
||||||
}
|
}
|
||||||
|
|
||||||
const credentials = await prisma.mailCredentials.findUnique({
|
// First try to get credentials from Redis cache
|
||||||
where: {
|
let credentials = await getCachedEmailCredentials(session.user.id);
|
||||||
userId: session.user.id
|
|
||||||
}
|
// If not in cache, get from database
|
||||||
});
|
if (!credentials) {
|
||||||
|
console.log(`Credentials not found in cache for ${session.user.id}, checking database`);
|
||||||
|
credentials = await prisma.mailCredentials.findUnique({
|
||||||
|
where: {
|
||||||
|
userId: session.user.id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!credentials) {
|
if (!credentials) {
|
||||||
throw new Error('No mail credentials found. Please configure your email account.');
|
throw new Error('No mail credentials found. Please configure your email account.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate credentials
|
||||||
|
if (!credentials.password) {
|
||||||
|
throw new Error('No password configured for email account');
|
||||||
|
}
|
||||||
|
|
||||||
return new ImapFlow({
|
return new ImapFlow({
|
||||||
host: credentials.host,
|
host: credentials.host,
|
||||||
port: credentials.port,
|
port: credentials.port,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user