diff --git a/.dockerignore b/.dockerignore index 847764c..4896aed 100644 --- a/.dockerignore +++ b/.dockerignore @@ -61,6 +61,3 @@ docker-compose.dev.yml # documentation README.md docs - -# scripts -scripts diff --git a/Dockerfile b/Dockerfile index ce14e20..47926fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,6 +37,7 @@ RUN pnpm prisma generate # Copy source files COPY src ./src COPY public ./public +COPY scripts ./scripts # Build the application RUN pnpm build @@ -64,6 +65,7 @@ COPY --from=builder /app/.next ./.next COPY --from=builder /app/public ./public COPY --from=builder /app/next-env.d.ts ./ COPY --from=builder /app/tailwind.config.ts ./ +COPY --from=builder /app/scripts ./scripts # Add non-root user for security RUN addgroup --system --gid 1001 nodejs && \ @@ -83,5 +85,5 @@ EXPOSE 3000 HEALTHCHECK --interval=30s --timeout=3s \ CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1 -# Start the application -CMD ["pnpm", "start"] \ No newline at end of file +# Start the application (init DB then start) +CMD ["pnpm", "start:prod"] \ No newline at end of file diff --git a/ENV.md b/ENV.md index 5b268e9..ff0c20b 100644 --- a/ENV.md +++ b/ENV.md @@ -11,6 +11,9 @@ MONGODB_URI=mongodb://admin:your-secure-password@mongodb:27017/stripstream?authS NEXTAUTH_SECRET=your-secret-key-here-generate-with-openssl-rand-base64-32 NEXTAUTH_URL=http://localhost:3020 +# Admin User (optional - default password for julienfroidefond@gmail.com) +ADMIN_DEFAULT_PASSWORD=Admin@2025 + # Node Environment NODE_ENV=production ``` diff --git a/package.json b/package.json index d4c848f..9bac198 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,8 @@ "dev": "next dev", "build": "next build", "start": "next start", + "start:prod": "node scripts/init-db.mjs && pnpm start", + "init-db": "node scripts/init-db.mjs", "lint": "next lint", "typecheck": "tsc --noEmit", "icons": "node scripts/generate-icons.js" diff --git a/scripts/init-db.mjs b/scripts/init-db.mjs new file mode 100755 index 0000000..53d7302 --- /dev/null +++ b/scripts/init-db.mjs @@ -0,0 +1,75 @@ +#!/usr/bin/env node +/** + * Script d'initialisation de la base de données + * Exécuté au démarrage de l'application + */ + +import { PrismaClient } from "@prisma/client"; +import bcrypt from "bcryptjs"; + +const prisma = new PrismaClient(); + +const ADMIN_EMAIL = "julienfroidefond@gmail.com"; +const ADMIN_PASSWORD = process.env.ADMIN_DEFAULT_PASSWORD || "Admin@2025"; + +async function initializeAdminUser() { + try { + // Vérifier si l'utilisateur existe déjà + const existingAdmin = await prisma.user.findUnique({ + where: { email: ADMIN_EMAIL }, + }); + + if (existingAdmin) { + // Vérifier si l'utilisateur a le rôle admin + const hasAdminRole = existingAdmin.roles.includes("ROLE_ADMIN"); + + if (hasAdminRole) { + console.log(`✅ Admin user ${ADMIN_EMAIL} already exists with admin role`); + } else { + // Ajouter le rôle admin + const updatedRoles = Array.from(new Set([...existingAdmin.roles, "ROLE_ADMIN"])); + await prisma.user.update({ + where: { email: ADMIN_EMAIL }, + data: { roles: updatedRoles }, + }); + console.log(`✅ Admin role added to ${ADMIN_EMAIL}`); + } + return; + } + + // Créer l'utilisateur admin + const hashedPassword = await bcrypt.hash(ADMIN_PASSWORD, 10); + await prisma.user.create({ + data: { + email: ADMIN_EMAIL, + password: hashedPassword, + roles: ["ROLE_USER", "ROLE_ADMIN"], + }, + }); + + console.log(`✅ Admin user created: ${ADMIN_EMAIL}`); + console.log(` Default password: ${ADMIN_PASSWORD}`); + console.log(` ⚠️ Please change the password after first login!`); + } catch (error) { + console.error("❌ Error initializing admin user:", error); + throw error; + } finally { + await prisma.$disconnect(); + } +} + +async function main() { + console.log("🔧 Initializing database..."); + + try { + await initializeAdminUser(); + console.log("✅ Database initialization completed"); + process.exit(0); + } catch (error) { + console.error("❌ Database initialization failed:", error); + process.exit(1); + } +} + +main(); +