From 99cb4e96f42f5daa3571831b1926605d7b225583 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 28 Apr 2025 14:27:08 +0200 Subject: [PATCH] courrier multi account restore compose --- app/api/courrier/session/route.ts | 2 +- lib/auth.ts | 57 ++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/app/api/courrier/session/route.ts b/app/api/courrier/session/route.ts index 0ca813d8..cc6f403c 100644 --- a/app/api/courrier/session/route.ts +++ b/app/api/courrier/session/route.ts @@ -1,6 +1,6 @@ import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; -import { authOptions } from '@/lib/auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { getMailboxes } from '@/lib/services/email-service'; import { getRedisClient } from '@/lib/redis'; import { getImapConnection } from '@/lib/services/email-service'; diff --git a/lib/auth.ts b/lib/auth.ts index c811cddc..8f89594f 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -1,5 +1,19 @@ import { NextAuthOptions } from 'next-auth'; import CredentialsProvider from 'next-auth/providers/credentials'; +import { prisma } from '@/lib/prisma'; + +// Extend the built-in User type +declare module "next-auth" { + interface User { + id: string; + email: string; + name?: string; + } + + interface Session { + user: User; + } +} export const authOptions: NextAuthOptions = { providers: [ @@ -14,19 +28,54 @@ export const authOptions: NextAuthOptions = { return null; } - // TODO: Implement actual authentication logic + const user = await prisma.user.findUnique({ + where: { email: credentials.email }, + select: { + id: true, + email: true, + password: true + } + }); + + if (!user) { + return null; + } + + // In production, you should use proper password hashing + if (user.password !== credentials.password) { + return null; + } + return { - id: '1', - email: credentials.email, - name: credentials.email.split('@')[0], + id: user.id, + email: user.email, + name: user.email.split('@')[0] }; } }) ], session: { strategy: 'jwt', + maxAge: 30 * 24 * 60 * 60, // 30 days + }, + jwt: { + maxAge: 30 * 24 * 60 * 60, // 30 days }, pages: { signIn: '/login', }, + callbacks: { + async jwt({ token, user }) { + if (user) { + token.id = user.id; + } + return token; + }, + async session({ session, token }) { + if (session.user) { + session.user.id = token.id as string; + } + return session; + } + } }; \ No newline at end of file