feat: integrate NextAuth.js for authentication, update database service to use better-sqlite3 adapter, and enhance header component with user session management

This commit is contained in:
Julien Froidefond
2025-11-27 13:08:09 +01:00
parent 68ef3731fa
commit 6a9bf88a65
15 changed files with 965 additions and 31 deletions

68
src/services/auth.ts Normal file
View File

@@ -0,0 +1,68 @@
import { hash } from 'bcryptjs';
import { prisma } from '@/services/database';
export interface RegisterInput {
email: string;
password: string;
name?: string;
}
export interface AuthResult {
success: boolean;
error?: string;
user?: {
id: string;
email: string;
name: string | null;
};
}
export async function registerUser(input: RegisterInput): Promise<AuthResult> {
const { email, password, name } = input;
// Check if user already exists
const existingUser = await prisma.user.findUnique({
where: { email },
});
if (existingUser) {
return {
success: false,
error: 'Un compte existe déjà avec cet email',
};
}
// Hash password
const hashedPassword = await hash(password, 12);
// Create user
const user = await prisma.user.create({
data: {
email,
password: hashedPassword,
name: name || null,
},
});
return {
success: true,
user: {
id: user.id,
email: user.email,
name: user.name,
},
};
}
export async function getUserByEmail(email: string) {
return prisma.user.findUnique({
where: { email },
});
}
export async function getUserById(id: string) {
return prisma.user.findUnique({
where: { id },
});
}

View File

@@ -1,18 +1,25 @@
import { PrismaClient } from '@prisma/client';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};
export const prisma =
globalForPrisma.prisma ??
new PrismaClient({
function createPrismaClient() {
const adapter = new PrismaBetterSqlite3({
url: process.env.DATABASE_URL ?? 'file:./prisma/dev.db',
});
return new PrismaClient({
adapter,
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
});
}
export const prisma = globalForPrisma.prisma ?? createPrismaClient();
if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma;
}
export default prisma;