mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 08:32:09 +01:00
- Add package.json with dependencies and scripts for Next.js and Prisma - Implement API routes for session management, user authentication, and company configuration - Create database schema for Company, User, and Session models in Prisma - Set up authentication with NextAuth and JWT - Add password reset functionality and user registration endpoint - Configure Tailwind CSS and PostCSS for styling - Implement metrics and dashboard settings API endpoints
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import NextAuth, { NextAuthOptions } from "next-auth";
|
|
import CredentialsProvider from "next-auth/providers/credentials";
|
|
import { prisma } from "../../../lib/prisma";
|
|
import bcrypt from "bcryptjs";
|
|
|
|
// Define the shape of the JWT token
|
|
declare module "next-auth/jwt" {
|
|
interface JWT {
|
|
companyId: string;
|
|
role: string;
|
|
}
|
|
}
|
|
|
|
// Define the shape of the session object
|
|
declare module "next-auth" {
|
|
interface Session {
|
|
user: {
|
|
id?: string;
|
|
name?: string;
|
|
email?: string;
|
|
image?: string;
|
|
companyId: string;
|
|
role: string;
|
|
};
|
|
}
|
|
|
|
interface User {
|
|
id: string;
|
|
email: string;
|
|
companyId: string;
|
|
role: string;
|
|
}
|
|
}
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
providers: [
|
|
CredentialsProvider({
|
|
name: "Credentials",
|
|
credentials: {
|
|
email: { label: "Email", type: "text" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials?.email || !credentials?.password) {
|
|
return null;
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: credentials.email }
|
|
});
|
|
|
|
if (!user) return null;
|
|
|
|
const valid = await bcrypt.compare(credentials.password, user.password);
|
|
if (!valid) return null;
|
|
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
companyId: user.companyId,
|
|
role: user.role,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
session: { strategy: "jwt" },
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.companyId = user.companyId;
|
|
token.role = user.role;
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
if (token && session.user) {
|
|
session.user.companyId = token.companyId;
|
|
session.user.role = token.role;
|
|
}
|
|
return session;
|
|
},
|
|
},
|
|
pages: {
|
|
signIn: "/login",
|
|
},
|
|
secret: process.env.NEXTAUTH_SECRET || "fallback-secret-key-change-in-production",
|
|
};
|
|
|
|
export default NextAuth(authOptions);
|