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 },
});
}