Files
livedash-node/lib/platform-auth.ts
Kaj Kowalski 93fbb44eec feat: comprehensive Biome linting fixes and code quality improvements
Major code quality overhaul addressing 58% of all linting issues:

• Type Safety Improvements:
  - Replace all any types with proper TypeScript interfaces
  - Fix Map component shadowing (renamed to CountryMap)
  - Add comprehensive custom error classes system
  - Enhance API route type safety

• Accessibility Enhancements:
  - Add explicit button types to all interactive elements
  - Implement useId() hooks for form element accessibility
  - Add SVG title attributes for screen readers
  - Fix static element interactions with keyboard handlers

• React Best Practices:
  - Resolve exhaustive dependencies warnings with useCallback
  - Extract nested component definitions to top level
  - Fix array index keys with proper unique identifiers
  - Improve component organization and prop typing

• Code Organization:
  - Automatic import organization and type import optimization
  - Fix unused function parameters and variables
  - Enhanced error handling with structured error responses
  - Improve component reusability and maintainability

Results: 248 → 104 total issues (58% reduction)
- Fixed all critical type safety and security issues
- Enhanced accessibility compliance significantly
- Improved code maintainability and performance
2025-06-29 07:35:45 +02:00

112 lines
2.7 KiB
TypeScript

import bcrypt from "bcryptjs";
import type { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { prisma } from "./prisma";
// 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) {
return null;
}
const platformUser = await prisma.platformUser.findUnique({
where: { email: credentials.email },
});
if (!platformUser) return null;
const valid = await bcrypt.compare(
credentials.password,
platformUser.password
);
if (!valid) return null;
return {
id: platformUser.id,
email: platformUser.email,
name: platformUser.name,
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",
};