diff --git a/backend/.env b/backend/.env index 91078d3..226bc31 100644 --- a/backend/.env +++ b/backend/.env @@ -16,7 +16,7 @@ AWS_ENDPOINT_URL=https://eu2.contabostorage.com # SMTP_HOST=smtp.gmail.com # SMTP_PORT=587 # SMTP_USER=taratur@gmail.com -# SMTP_PASS=dziy nccc svgg bovb +# SMTP_PASS=dziy nccc svgg bovb # EMAIL_FROM=taratur@gmail.com SMTP_HOST=imk.mk @@ -25,7 +25,13 @@ SMTP_USER=mailer@imk.mk SMTP_PASS=76Avtostoperski76 SMTP_FROM=mailer@imk.mk # FRONTEND_URL=https://imk.mk -# EMAIL_FROM=petrovskidimitar@yandex.com +EMAIL_FROM=mailer@yandex.com ADMIN_EMAIL=taratur@gmail.com + + +# default app ADMIN +DEFAULT_ADMIN_EMAIL=taratur@gmail.com +DEFAULT_ADMIN_PASSWORD=irina7654321 +DEFAULT_ADMIN_NAME=admin diff --git a/backend/README.md b/backend/README.md index 00a13b1..495c7f7 100644 --- a/backend/README.md +++ b/backend/README.md @@ -1,73 +1,47 @@ -
+ Add the necessary environment variables to your `.env` file: -[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 -[circleci-url]: https://circleci.com/gh/nestjs/nest - -A progressive Node.js framework for building efficient and scalable server-side applications.
- - - -## Description - -[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. - -## Installation - -```bash -$ npm install +```env +# Default Admin Configuration +DEFAULT_ADMIN_EMAIL=admin@imk.com +DEFAULT_ADMIN_PASSWORD=admin123456 +DEFAULT_ADMIN_NAME=System Admin ``` -## Running the app +To use this setup: +1. The seed script will run automatically during deployment when you run: ```bash -# development -$ npm run start - -# watch mode -$ npm run start:dev - -# production mode -$ npm run start:prod +npx prisma db push +npx prisma db seed ``` -## Test - -```bash -# unit tests -$ npm run test - -# e2e tests -$ npm run test:e2e - -# test coverage -$ npm run test:cov +2. Alternatively, you can manually trigger the initialization by making a POST request to: +``` +POST /init/system ``` -## Support +This gives you two ways to ensure the default admin user is created: +1. Automatically during deployment via the seed script +2. Manually via the initialization endpoint -Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). +To deploy, you would: -## Stay in touch +1. Set up your environment variables +2. Run the database migrations +3. Run the seed script +```bash +npm run prisma:deploy +npm run prisma:seed +``` -- Author - [Kamil MyĆliwiec](https://kamilmysliwiec.com) -- Website - [https://nestjs.com](https://nestjs.com/) -- Twitter - [@nestframework](https://twitter.com/nestframework) +The default admin credentials will be: +- Email: admin@imk.com (or whatever you set in env) +- Password: admin123456 (or whatever you set in env) -## License +Make sure to: +1. Change the default password after first login +2. Use strong passwords in production +3. Properly secure the initialization endpoint in production +4. Keep your environment variables secure -Nest is [MIT licensed](LICENSE). +This setup ensures you always have an admin user available after deployment while maintaining security and flexibility. diff --git a/backend/package.json b/backend/package.json index b58704f..cbf12a2 100644 --- a/backend/package.json +++ b/backend/package.json @@ -17,7 +17,8 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json" + "test:e2e": "jest --config ./test/jest-e2e.json", + "prisma:seed": "ts-node prisma/seed.ts" }, "dependencies": { "@aws-sdk/client-s3": "^3.679.0", diff --git a/backend/prisma/seed.ts b/backend/prisma/seed.ts index ddae7b9..fddd859 100644 --- a/backend/prisma/seed.ts +++ b/backend/prisma/seed.ts @@ -1,23 +1,47 @@ -import { PrismaClient } from '@prisma/client'; -import * as bcrypt from 'bcrypt'; +// backend/prisma/seed.ts +import { PrismaClient } from "@prisma/client"; +import * as bcrypt from "bcrypt"; const prisma = new PrismaClient(); async function main() { - const hashedPassword = await bcrypt.hash('admin123', 10); + const defaultAdminEmail = process.env.DEFAULT_ADMIN_EMAIL || "admin@imk.com"; + const defaultAdminPassword = + process.env.DEFAULT_ADMIN_PASSWORD || "admin123456"; + const defaultAdminName = process.env.DEFAULT_ADMIN_NAME || "System Admin"; - const admin = await prisma.user.upsert({ - where: { email: 'admin@example.com' }, - update: {}, - create: { - email: 'admin@example.com', - name: 'Admin User', - password: hashedPassword, - isAdmin: true, - }, - }); + try { + // Check if admin already exists + const existingAdmin = await prisma.user.findUnique({ + where: { email: defaultAdminEmail }, + }); - console.log({ admin }); + if (!existingAdmin) { + // Hash the password + const hashedPassword = await bcrypt.hash(defaultAdminPassword, 10); + + // Create the admin user + const admin = await prisma.user.create({ + data: { + email: defaultAdminEmail, + name: defaultAdminName, + password: hashedPassword, + isAdmin: true, + }, + }); + + console.log("Default admin user created:", { + id: admin.id, + email: admin.email, + name: admin.name, + }); + } else { + console.log("Default admin user already exists"); + } + } catch (error) { + console.error("Error creating default admin:", error); + throw error; + } } main() diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index f6d3754..751f184 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -1,39 +1,31 @@ -import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; -import { AuthModule } from './auth/auth.module'; +import { Module } from "@nestjs/common"; +import { AppController } from "./app.controller"; +import { AppService } from "./app.service"; +import { AuthModule } from "./auth/auth.module"; //import { TypeOrmModule } from '@nestjs/typeorm'; -import { AdminModule } from './admin/admin.module'; -import { ClientModule } from './client/client.module'; -import { UploadService } from './upload/upload.service'; -import { DocumentsService } from './documents/documents.service'; -import { S3Service } from './s3/s3.service'; -import { S3Module } from './s3/s3.module'; -import { PrismaService } from './prisma/prisma.service'; -import { PrismaModule } from './prisma/prisma.module'; -import { ConfigModule } from '@nestjs/config'; -import { AuthController } from './auth/auth.controller'; -import { DocumentsController } from './documents/documents.controller'; -import { JwtModule } from '@nestjs/jwt'; -import { EmailModule } from './email/email.module'; +import { AdminModule } from "./admin/admin.module"; +import { ClientModule } from "./client/client.module"; +import { UploadService } from "./upload/upload.service"; +import { DocumentsService } from "./documents/documents.service"; +import { S3Service } from "./s3/s3.service"; +import { S3Module } from "./s3/s3.module"; +import { PrismaService } from "./prisma/prisma.service"; +import { PrismaModule } from "./prisma/prisma.module"; +import { ConfigModule } from "@nestjs/config"; +import { AuthController } from "./auth/auth.controller"; +import { DocumentsController } from "./documents/documents.controller"; +import { JwtModule } from "@nestjs/jwt"; +import { EmailModule } from "./email/email.module"; +import { InitModule } from "./init/init.module"; @Module({ imports: [ - // TypeOrmModule.forRoot({ - // type: 'postgres', - // host: 'localhost', - // port: 5432, - // username: 'root', - // password: 'admin', - // database: 'imk', - // synchronize: true, - // }), ConfigModule.forRoot({ isGlobal: true, }), JwtModule.register({ secret: process.env.JWT_SECRET, - signOptions: { expiresIn: '1h' }, + signOptions: { expiresIn: "1h" }, }), AuthModule, AdminModule, @@ -41,6 +33,7 @@ import { EmailModule } from './email/email.module'; S3Module, PrismaModule, EmailModule, + InitModule, ], controllers: [AppController, AuthController, DocumentsController], providers: [ diff --git a/backend/src/email/email.service.ts b/backend/src/email/email.service.ts index 993c3de..a3cb526 100644 --- a/backend/src/email/email.service.ts +++ b/backend/src/email/email.service.ts @@ -1,6 +1,6 @@ -import { Injectable, Logger } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; -import * as nodemailer from 'nodemailer'; +import { Injectable, Logger } from "@nestjs/common"; +import { ConfigService } from "@nestjs/config"; +import * as nodemailer from "nodemailer"; @Injectable() export class EmailService { @@ -9,29 +9,26 @@ export class EmailService { private readonly from: string; constructor(private configService: ConfigService) { - console.log('Initializing EmailService...'); // Direct console log for debugging - this.logger.log('Initializing EmailService...'); + console.log("Initializing EmailService..."); // Direct console log for debugging + this.logger.log("Initializing EmailService..."); // Load config - const host = this.configService.getIf you have any questions or need assistance, please don't hesitate to contact our support team.
Best regards,
The IMK Team