diff --git a/.env b/.env index 4c15dc5..d0b9db4 100644 --- a/.env +++ b/.env @@ -3,3 +3,6 @@ PGADMIN_DEFAULT_PASSWORD: fakepassword123! POSTGRES_USER=root POSTGRES_PASSWORD=root POSTGRES_DB=root +AUTH_SOURCE_URL=postgresql://postgres:postgres@localhost:5444/user-authentication +PUBLIC_KEY_PATH=tokens/jwt.key.pub +PRIVATE_KEY_PATH=tokens/jwt.key diff --git a/README.md b/README.md index b94562f..1a5929d 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,33 @@ To start the development server run `nx serve frontend`. Open your browser and navigate to http://localhost:4200/. Happy coding! -Run all services: + +```bash +npm i -g prisma nx +``` + +```bash +yarn install +``` + +```bash +copy .env.example .env +``` + +```bash +cd apps/auth/serving && copy database.env.example database.env && sudo docker-compose up -d +``` + +```bash +prisma generate --schema="./libs/prisma-clients/auth/prisma/schema.prisma" ``` + +```bash +prisma db push --schema="./libs/prisma-clients/auth/prisma/schema.prisma" +``` + +Run all services: +```bash nx run-many --parallel --target=serve --projects=agw,user-data,auth,frontend ``` diff --git a/apps/auth/.env.example b/apps/auth/.env.example deleted file mode 100644 index fc84cb9..0000000 --- a/apps/auth/.env.example +++ /dev/null @@ -1,6 +0,0 @@ -PORT=3005 - -DATABASE_URL=postgresql://postgres:postgres@localhost:5433/user-authentication - -PUBLIC_KEY_PATH=tokens/jwt.key.pub -PRIVATE_KEY_PATH=tokens/jwt.key diff --git a/apps/auth/Dockerfile b/apps/auth/Dockerfile deleted file mode 100644 index f0b43cf..0000000 --- a/apps/auth/Dockerfile +++ /dev/null @@ -1,32 +0,0 @@ -# syntax=docker/dockerfile:1 -FROM node:18 - -WORKDIR /app - -COPY --chown=node:node . . - -RUN rm -f *.env *.env.* - -RUN apt-get update -y && apt-get install -y dumb-init - -RUN npm install - -ENV NODE_ENV=production - -RUN npm run prefullbuild && npm run prebuild && npm run build - -RUN mkdir temp temp/.prisma temp/@prisma temp/prisma && cp -r ./node_modules/.prisma/* ./temp/.prisma/ && cp -r ./node_modules/@prisma/* ./temp/@prisma/ && cp -r ./node_modules/prisma/* ./temp/prisma/ - -RUN rm -rdf node_modules - -RUN npm install --production - -RUN cp -r ./temp/* ./node_modules/ && rm -rdf temp - -RUN ls | grep -v node_modules | grep -v dist | xargs rm -rfv - -RUN cp -r ./dist/* ./ && rm -rdf dist - -USER node - -CMD ["dumb-init", "node", "./main.js"] diff --git a/apps/auth/prisma/.gitignore b/apps/auth/prisma/.gitignore deleted file mode 100644 index 24a8e4d..0000000 --- a/apps/auth/prisma/.gitignore +++ /dev/null @@ -1 +0,0 @@ -migrations diff --git a/apps/auth/serving/docker-compose.yaml b/apps/auth/serving/docker-compose.yaml index 1c0a532..63f72f7 100644 --- a/apps/auth/serving/docker-compose.yaml +++ b/apps/auth/serving/docker-compose.yaml @@ -5,24 +5,14 @@ services: image: postgres:latest container_name: user-authentication-database restart: always + ports: + - 5444:5432 networks: - user-authentication volumes: - user-authentication-database:/var/lib/postgresql env_file: - ./database.env - user-authentication-database-admin: - container_name: user-authentication-database-admin - image: bitnami/phppgadmin:latest - restart: always - networks: - - user-authentication - depends_on: - - user-authentication-database - environment: - - DATABASE_HOST=user-authentication-database - ports: - - "8083:8080" networks: user-authentication: diff --git a/apps/auth/src/device/service/device.service.ts b/apps/auth/src/device/service/device.service.ts index 5e0e462..7f156cb 100644 --- a/apps/auth/src/device/service/device.service.ts +++ b/apps/auth/src/device/service/device.service.ts @@ -1,11 +1,11 @@ import { BadRequestException, ConflictException, Injectable } from "@nestjs/common"; -import { PrismaService } from "../../Prisma/service/prisma.service"; import { UserDto } from "../dto/user.dto"; import { PrismaClientValidationError } from "@prisma/client/runtime"; +import { AuthClient } from "@platform/prisma-clients"; @Injectable() export class DeviceService { - constructor(private readonly prisma: PrismaService) { + constructor(private readonly prisma: AuthClient) { } public getUserUuidAsPrismaPromise(params: { fingerprint: string }) { diff --git a/apps/auth/src/user/service/user.service.ts b/apps/auth/src/user/service/user.service.ts index 0c9008d..73f22f5 100644 --- a/apps/auth/src/user/service/user.service.ts +++ b/apps/auth/src/user/service/user.service.ts @@ -1,11 +1,11 @@ import { BadRequestException, Injectable } from "@nestjs/common"; -import { PrismaService } from "../../Prisma/service/prisma.service"; +import { AuthClient } from "@platform/prisma-clients"; import { DevicesDto } from "../dto/devices.dto"; import { PrismaClientValidationError } from "@prisma/client/runtime"; @Injectable() export class UserService { - constructor(private readonly prisma: PrismaService) { + constructor(private readonly prisma: AuthClient) { } public async devices(params: DevicesDto): Promise<{ name?: string, fingerprint?: string, isBlocked?: boolean }[]> { @@ -51,4 +51,4 @@ export class UserService { public isBlocked(params: { uuid: string }) { return this.prisma.user.findUnique({ where: params, select: { isBlocked: true } }); } -} \ No newline at end of file +} diff --git a/apps/auth/tokens.example/.gitkeep b/apps/auth/tokens.example/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/libs/prisma-clients/auth/.gitignore b/libs/prisma-clients/auth/.gitignore new file mode 100644 index 0000000..11ddd8d --- /dev/null +++ b/libs/prisma-clients/auth/.gitignore @@ -0,0 +1,3 @@ +node_modules +# Keep environment variables out of version control +.env diff --git a/libs/prisma-clients/auth/index.ts b/libs/prisma-clients/auth/index.ts new file mode 100644 index 0000000..8d3ae02 --- /dev/null +++ b/libs/prisma-clients/auth/index.ts @@ -0,0 +1,2 @@ +export { PrismaClient as AuthClient } from '.prisma/auth-client'; +export * from '.prisma/auth-client'; diff --git a/libs/prisma-clients/auth/prisma/error/record-not-found.error.ts b/libs/prisma-clients/auth/prisma/error/record-not-found.error.ts new file mode 100644 index 0000000..e1c9cb1 --- /dev/null +++ b/libs/prisma-clients/auth/prisma/error/record-not-found.error.ts @@ -0,0 +1 @@ +export class RecordNotFoundError extends Error {} diff --git a/libs/prisma-clients/auth/prisma/error/unique-constraint-failed.error.ts b/libs/prisma-clients/auth/prisma/error/unique-constraint-failed.error.ts new file mode 100644 index 0000000..647b582 --- /dev/null +++ b/libs/prisma-clients/auth/prisma/error/unique-constraint-failed.error.ts @@ -0,0 +1 @@ +export class UniqueConstraintFailedError extends Error {} diff --git a/libs/prisma-clients/auth/prisma/interceptor/prisma-error.interceptor.ts b/libs/prisma-clients/auth/prisma/interceptor/prisma-error.interceptor.ts new file mode 100644 index 0000000..d61d135 --- /dev/null +++ b/libs/prisma-clients/auth/prisma/interceptor/prisma-error.interceptor.ts @@ -0,0 +1,26 @@ +import { + CallHandler, + ConflictException, + ExecutionContext, + Injectable, + NestInterceptor, + NotFoundException, +} from '@nestjs/common'; +import { catchError, Observable } from 'rxjs'; +import { RecordNotFoundError } from '../error/record-not-found.error'; +import { UniqueConstraintFailedError } from '../error/unique-constraint-failed.error'; + +@Injectable() +export class PrismaErrorInterceptor implements NestInterceptor { + intercept(context: ExecutionContext, next: CallHandler): Observable { + return next.handle().pipe( + catchError((err) => { + if (err instanceof RecordNotFoundError) + throw new NotFoundException(err.message); + if (err instanceof UniqueConstraintFailedError) + throw new ConflictException(err.message); + throw err; + }) + ); + } +} diff --git a/apps/auth/prisma/schema.prisma b/libs/prisma-clients/auth/prisma/schema.prisma similarity index 86% rename from apps/auth/prisma/schema.prisma rename to libs/prisma-clients/auth/prisma/schema.prisma index 2baa5df..b392c55 100644 --- a/apps/auth/prisma/schema.prisma +++ b/libs/prisma-clients/auth/prisma/schema.prisma @@ -3,11 +3,12 @@ generator client { provider = "prisma-client-js" + output = "../../../../node_modules/.prisma/auth-client" } datasource db { provider = "postgresql" - url = env("DATABASE_URL") + url = env("AUTH_SOURCE_URL") } model User { diff --git a/libs/prisma-clients/index.ts b/libs/prisma-clients/index.ts new file mode 100644 index 0000000..086aa34 --- /dev/null +++ b/libs/prisma-clients/index.ts @@ -0,0 +1 @@ +export { AuthClient } from './auth'; diff --git a/tsconfig.base.json b/tsconfig.base.json index 8cb69ea..703948f 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -17,6 +17,7 @@ "paths": { "@pconfig": ["config/index.ts"], "@platform/prisma-generator": ["libs/prisma-generator/src/index.ts"], + "@platform/prisma-clients": ["libs/prisma-clients/index.ts"], "@platform/ui": ["libs/ui/src/index.ts"] } },