From fdb1e26367493e62d54097fc8dd6802ff80affc9 Mon Sep 17 00:00:00 2001 From: Aleksi Lassila Date: Sun, 9 Jun 2024 01:30:06 +0300 Subject: [PATCH] feat: Create admin account with environment variables, set secret, fix build issue --- .dockerignore | 1 - Dockerfile | 3 --- README.md | 6 ++++++ backend/src/consts.ts | 5 ++++- backend/src/main.ts | 15 +++++++++++++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.dockerignore b/.dockerignore index cc722c7..be079a6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,6 @@ **/node_modules .svelte-kit build -dist .idea .env .DS_Store diff --git a/Dockerfile b/Dockerfile index e5286f8..54cec90 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,8 +20,6 @@ COPY . . RUN npm run build -RUN npm run build --prefix backend - FROM --platform=linux/amd64 node:18-alpine as production RUN mkdir -p /usr/src/app @@ -31,7 +29,6 @@ ENV NODE_ENV=production COPY --from=pre-production /usr/src/app/backend/dist ./dist COPY --from=pre-production /usr/src/app/backend/node_modules ./node_modules -COPY --from=pre-production /usr/src/app/dist ./dist/dist COPY backend/package.json . COPY backend/package-lock.json . diff --git a/README.md b/README.md index 2bdbc40..61363bf 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,10 @@ services: container_name: reiverr ports: - 9494:9494 + environment: + - SECRET=your_secret_here # optional, used to sign JWT tokens for authentication. If not set, sessions will not persist between server restarts. Use a random string. + - ADMIN_USERNAME=admin # optional + - ADMIN_PASSWORD=admin # optional volumes: - /path/to/appdata/config:/config restart: unless-stopped @@ -115,6 +119,8 @@ The roadmap includes plans to support the following platforms in the future: # Post Installation To create the first user account, you can log in with any credentials and an admin account will be created. +Alternatively, you can define the admin username and password using environment variables, +as seen in the Docker Compose example. A new admin account is only created if there are no previous accounts with the same name. To get most out of Reiverr, it is recommended to connect to TMDB, Jellyfin, Radarr and Sonarr. > Hint: Radarr & Sonarr API keys can be found under Settings > General in their respective web UIs. Jellyfin API key is located under Administration > Dashboard > Advanced > API Keys in the Jellyfin Web UI. diff --git a/backend/src/consts.ts b/backend/src/consts.ts index 37daa38..4b5c05e 100644 --- a/backend/src/consts.ts +++ b/backend/src/consts.ts @@ -1 +1,4 @@ -export const JWT_SECRET = Math.random().toString(36).substring(2, 15); +export const JWT_SECRET = + process.env.SECRET || Math.random().toString(36).substring(2, 15); +export const ADMIN_USERNAME = process.env.ADMIN_USERNAME; +export const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD; diff --git a/backend/src/main.ts b/backend/src/main.ts index 83e3e91..13439cf 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -3,8 +3,20 @@ import { AppModule } from './app.module'; import 'reflect-metadata'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import * as fs from 'fs'; +import { UserService } from './user/user.service'; +import { ADMIN_PASSWORD, ADMIN_USERNAME } from './consts'; // import * as proxy from 'express-http-proxy'; +async function createAdminUser(userService: UserService) { + if (!ADMIN_USERNAME || ADMIN_PASSWORD === undefined) return; + + const existingUser = await userService.findOneByName(ADMIN_USERNAME); + + if (!existingUser) { + await userService.create(ADMIN_USERNAME, ADMIN_PASSWORD, true); + } +} + async function bootstrap() { const app = await NestFactory.create(AppModule); app.setGlobalPrefix('api'); @@ -20,6 +32,9 @@ async function bootstrap() { SwaggerModule.setup('openapi', app, document); fs.writeFileSync('./swagger-spec.json', JSON.stringify(document)); + await createAdminUser(app.get(UserService)); + await app.listen(9494); } + bootstrap();