Files
Momento/memento-note/auth.ts
Antigravity 5b794d6449
Some checks failed
CI / Lint, Test & Build (push) Failing after 7m46s
CI / Deploy production (on server) (push) Has been cancelled
feat(auth): restore Google sign-in and AI admin test routes
Google OAuth was implemented locally but never deployed; the login button
only renders when AUTH_GOOGLE_ID and AUTH_GOOGLE_SECRET are set. Also
restores /api/ai/test-* endpoints removed by mistake and wires Google
credentials into deploy workflows.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-17 17:17:42 +00:00

61 lines
1.9 KiB
TypeScript

import NextAuth from 'next-auth';
import { PrismaAdapter } from '@auth/prisma-adapter';
import { authConfig } from './auth.config';
import prisma from '@/lib/prisma';
import { buildAuthProviders } from '@/lib/auth-providers';
export const { auth, signIn, signOut, handlers } = NextAuth({
...authConfig,
adapter: PrismaAdapter(prisma),
providers: buildAuthProviders(),
events: {
async createUser({ user }) {
const adminEmail = process.env.ADMIN_EMAIL?.toLowerCase();
if (!adminEmail || !user.id || user.email?.toLowerCase() !== adminEmail) {
return;
}
await prisma.user.update({
where: { id: user.id },
data: { role: 'ADMIN', emailVerified: new Date() },
});
},
},
callbacks: {
...authConfig.callbacks,
async signIn({ user, account }) {
if (account?.provider === 'google' && user.email) {
const email = user.email.toLowerCase();
const adminEmail = process.env.ADMIN_EMAIL?.toLowerCase();
const existing = await prisma.user.findUnique({ where: { email } });
if (existing && adminEmail && email === adminEmail && existing.role !== 'ADMIN') {
await prisma.user.update({
where: { id: existing.id },
data: { role: 'ADMIN', emailVerified: new Date() },
});
}
}
return true;
},
async jwt({ token, user }) {
if (user?.id) {
token.id = user.id;
const dbUser = await prisma.user.findUnique({
where: { id: user.id },
select: { role: true },
});
token.role = dbUser?.role ?? 'USER';
} else if (token.sub) {
const dbUser = await prisma.user.findUnique({
where: { id: token.sub },
select: { role: true },
});
if (dbUser) {
token.id = token.sub;
token.role = dbUser.role;
}
}
return token;
},
},
});