mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 18:12:08 +01:00
feat: Add authentication and session management with NextAuth.js and Prisma [broken]
- Implemented API session retrieval in `lib/api-auth.ts` to manage user sessions. - Created authentication options in `lib/auth-options.ts` using NextAuth.js with credentials provider. - Added migration scripts to create necessary tables for authentication in `migrations/0002_create_auth_tables.sql` and `prisma/migrations/20250601033219_add_nextauth_tables/migration.sql`. - Configured ESLint with Next.js and TypeScript support in `eslint.config.mjs`. - Updated Next.js configuration in `next.config.ts` for Cloudflare compatibility. - Defined Cloudflare Worker configuration in `open-next.config.ts` and `wrangler.jsonc`. - Enhanced type definitions for authentication in `types/auth.d.ts`. - Created a Cloudflare Worker entry point in `src/index.ts.backup` to handle API requests and responses.
This commit is contained in:
115
app/api/auth/[...nextauth]/route.ts
Normal file
115
app/api/auth/[...nextauth]/route.ts
Normal file
@ -0,0 +1,115 @@
|
||||
import NextAuth, { NextAuthConfig } from "next-auth";
|
||||
import { D1Adapter } from "@auth/d1-adapter";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import * as bcrypt from "bcryptjs";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { PrismaD1 } from "@prisma/adapter-d1";
|
||||
|
||||
// Check if we're in a Cloudflare Workers environment
|
||||
const isCloudflareWorker =
|
||||
typeof globalThis.caches !== "undefined" &&
|
||||
typeof (globalThis as any).WebSocketPair !== "undefined";
|
||||
|
||||
const config: NextAuthConfig = {
|
||||
providers: [
|
||||
Credentials({
|
||||
name: "credentials",
|
||||
credentials: {
|
||||
email: { label: "Email", type: "email" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
authorize: async (credentials) => {
|
||||
if (!credentials?.email || !credentials?.password) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
let prisma: PrismaClient;
|
||||
|
||||
// Initialize Prisma based on environment
|
||||
if (isCloudflareWorker) {
|
||||
// In Cloudflare Workers, get DB from bindings
|
||||
const adapter = new PrismaD1((globalThis as any).DB);
|
||||
prisma = new PrismaClient({ adapter });
|
||||
} else {
|
||||
// In local development, use standard Prisma
|
||||
prisma = new PrismaClient();
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email: credentials.email as string },
|
||||
include: { company: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
await prisma.$disconnect();
|
||||
return null;
|
||||
}
|
||||
|
||||
const valid = await bcrypt.compare(
|
||||
credentials.password as string,
|
||||
user.password
|
||||
);
|
||||
|
||||
if (!valid) {
|
||||
await prisma.$disconnect();
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.email, // Use email as name
|
||||
role: user.role,
|
||||
companyId: user.companyId,
|
||||
company: user.company.name,
|
||||
};
|
||||
|
||||
await prisma.$disconnect();
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("Authentication error:", error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
jwt: async ({ token, user }: any) => {
|
||||
if (user) {
|
||||
token.role = user.role;
|
||||
token.companyId = user.companyId;
|
||||
token.company = user.company;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
session: async ({ session, token }: any) => {
|
||||
if (token && session.user) {
|
||||
session.user.id = token.sub;
|
||||
session.user.role = token.role;
|
||||
session.user.companyId = token.companyId;
|
||||
session.user.company = token.company;
|
||||
}
|
||||
return session;
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
error: "/login",
|
||||
},
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
maxAge: 30 * 24 * 60 * 60, // 30 days
|
||||
},
|
||||
secret: process.env.AUTH_SECRET,
|
||||
trustHost: true,
|
||||
};
|
||||
|
||||
// Add D1 adapter only in Cloudflare Workers environment
|
||||
if (isCloudflareWorker && (globalThis as any).DB) {
|
||||
(config as any).adapter = D1Adapter((globalThis as any).DB);
|
||||
}
|
||||
|
||||
const { handlers } = NextAuth(config);
|
||||
|
||||
export const { GET, POST } = handlers;
|
||||
Reference in New Issue
Block a user