routes fix

This commit is contained in:
2026-03-12 19:47:28 -05:00
parent 6b0190de81
commit ebc0c61da4
7 changed files with 51 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
// GET /api/admin/settings - Fetch global settings // GET /api/admin/settings - Fetch global settings

View File

@@ -1,48 +1,5 @@
import NextAuth, { NextAuthOptions } from "next-auth"; import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google"; import { authOptions } from "@/lib/auth";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "@/lib/prisma";
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
],
callbacks: {
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token;
}
return token;
},
async session({ session, user, token }) {
if (session.user) {
session.user.id = user?.id || (token?.sub as string);
// Fetch fresh role from DB if needed, or use token
const dbUser = await prisma.user.findUnique({ where: { id: session.user.id } });
session.user.role = dbUser?.role || 'EMPLOYEE';
}
return session;
},
},
events: {
async createUser({ user }) {
const userCount = await prisma.user.count();
if (userCount === 1) {
await prisma.user.update({
where: { id: user.id },
data: { role: 'ADMIN' },
});
}
},
},
pages: {
signIn: "/auth/signin",
},
};
const handler = NextAuth(authOptions); const handler = NextAuth(authOptions);

View File

@@ -1,6 +1,6 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { uploadToDrive, generateReportMarkdown } from "@/lib/google-drive"; import { uploadToDrive, generateReportMarkdown } from "@/lib/google-drive";
import { getToken } from "next-auth/jwt"; import { getToken } from "next-auth/jwt";

View File

@@ -1,6 +1,6 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
// PATCH /api/reports/[id] - Update report status or manager // PATCH /api/reports/[id] - Update report status or manager

View File

@@ -1,6 +1,6 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
// GET /api/reports - Fetch reports for the logged-in user (or all for admin) // GET /api/reports - Fetch reports for the logged-in user (or all for admin)

View File

@@ -1,6 +1,6 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
// POST /api/tasks - Add a task to a report // POST /api/tasks - Add a task to a report

44
src/lib/auth.ts Normal file
View File

@@ -0,0 +1,44 @@
import { NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "@/lib/prisma";
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
],
callbacks: {
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token;
}
return token;
},
async session({ session, user, token }) {
if (session?.user) {
session.user.id = user?.id || (token?.sub as string);
const dbUser = await prisma.user.findUnique({ where: { id: session.user.id } });
session.user.role = dbUser?.role || 'EMPLOYEE';
}
return session;
},
},
events: {
async createUser({ user }) {
const userCount = await prisma.user.count();
if (userCount === 1) {
await prisma.user.update({
where: { id: user.id },
data: { role: 'ADMIN' },
});
}
},
},
pages: {
signIn: "/auth/signin",
},
};