mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 21:12:08 +01:00
- Fix 36+ biome linting issues reducing errors/warnings from 227 to 191 - Replace explicit 'any' types with proper TypeScript interfaces - Fix React hooks dependencies and useCallback patterns - Resolve unused variables and parameter assignment issues - Improve accessibility with proper label associations - Add comprehensive API documentation for admin and security features - Update README.md with accurate PostgreSQL setup and current tech stack - Create complete documentation for audit logging, CSP monitoring, and batch processing - Fix outdated project information and missing developer workflows
170 lines
4.4 KiB
TypeScript
170 lines
4.4 KiB
TypeScript
import bcrypt from "bcryptjs";
|
|
import type { NextAuthOptions } from "next-auth";
|
|
import CredentialsProvider from "next-auth/providers/credentials";
|
|
import { prisma } from "./prisma";
|
|
import {
|
|
AuditOutcome,
|
|
createAuditMetadata,
|
|
securityAuditLogger,
|
|
} from "./securityAuditLogger";
|
|
|
|
// Define the shape of the JWT token for platform users
|
|
declare module "next-auth/jwt" {
|
|
interface JWT {
|
|
isPlatformUser?: boolean;
|
|
platformRole?: string;
|
|
}
|
|
}
|
|
|
|
// Define the shape of the session object for platform users
|
|
declare module "next-auth" {
|
|
interface Session {
|
|
user: {
|
|
id?: string;
|
|
name?: string;
|
|
email?: string;
|
|
image?: string;
|
|
isPlatformUser?: boolean;
|
|
platformRole?: string;
|
|
companyId?: string;
|
|
role?: string;
|
|
};
|
|
}
|
|
|
|
interface User {
|
|
id: string;
|
|
email: string;
|
|
name?: string;
|
|
isPlatformUser?: boolean;
|
|
platformRole?: string;
|
|
companyId?: string;
|
|
role?: string;
|
|
}
|
|
}
|
|
|
|
export const platformAuthOptions: NextAuthOptions = {
|
|
providers: [
|
|
CredentialsProvider({
|
|
name: "Platform Credentials",
|
|
credentials: {
|
|
email: { label: "Email", type: "text" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials?.email || !credentials?.password) {
|
|
await securityAuditLogger.logPlatformAdmin(
|
|
"platform_login_attempt",
|
|
AuditOutcome.FAILURE,
|
|
{
|
|
metadata: createAuditMetadata({
|
|
error: "missing_credentials",
|
|
email: credentials?.email ? "[REDACTED]" : "missing",
|
|
}),
|
|
},
|
|
"Missing email or password for platform login"
|
|
);
|
|
return null;
|
|
}
|
|
|
|
const platformUser = await prisma.platformUser.findUnique({
|
|
where: { email: credentials.email },
|
|
});
|
|
|
|
if (!platformUser) {
|
|
await securityAuditLogger.logPlatformAdmin(
|
|
"platform_login_attempt",
|
|
AuditOutcome.FAILURE,
|
|
{
|
|
metadata: createAuditMetadata({
|
|
error: "user_not_found",
|
|
email: "[REDACTED]",
|
|
}),
|
|
},
|
|
"Platform user not found"
|
|
);
|
|
return null;
|
|
}
|
|
|
|
const valid = await bcrypt.compare(
|
|
credentials.password,
|
|
platformUser.password
|
|
);
|
|
|
|
if (!valid) {
|
|
await securityAuditLogger.logPlatformAdmin(
|
|
"platform_login_attempt",
|
|
AuditOutcome.FAILURE,
|
|
{
|
|
platformUserId: platformUser.id,
|
|
metadata: createAuditMetadata({
|
|
error: "invalid_password",
|
|
email: "[REDACTED]",
|
|
role: platformUser.role,
|
|
}),
|
|
},
|
|
"Invalid password for platform login"
|
|
);
|
|
return null;
|
|
}
|
|
|
|
// Log successful platform authentication
|
|
await securityAuditLogger.logPlatformAdmin(
|
|
"platform_login_success",
|
|
AuditOutcome.SUCCESS,
|
|
{
|
|
platformUserId: platformUser.id,
|
|
metadata: createAuditMetadata({
|
|
role: platformUser.role,
|
|
name: platformUser.name,
|
|
}),
|
|
}
|
|
);
|
|
|
|
return {
|
|
id: platformUser.id,
|
|
email: platformUser.email,
|
|
name: platformUser.name || undefined,
|
|
isPlatformUser: true,
|
|
platformRole: platformUser.role,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
session: {
|
|
strategy: "jwt",
|
|
maxAge: 8 * 60 * 60, // 8 hours for platform users (more secure)
|
|
},
|
|
cookies: {
|
|
sessionToken: {
|
|
name: "platform-auth.session-token",
|
|
options: {
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
path: "/",
|
|
secure: process.env.NODE_ENV === "production",
|
|
},
|
|
},
|
|
},
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.isPlatformUser = user.isPlatformUser;
|
|
token.platformRole = user.platformRole;
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
if (token && session.user) {
|
|
session.user.isPlatformUser = token.isPlatformUser;
|
|
session.user.platformRole = token.platformRole;
|
|
}
|
|
return session;
|
|
},
|
|
},
|
|
pages: {
|
|
signIn: "/platform/login",
|
|
},
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
debug: process.env.NODE_ENV === "development",
|
|
};
|