The Ansopedia User Service is a backend service responsible for managing user accounts and authentication within the Ansopedia learning platform.
It provides functionalities like:
- User Registration and Login: Create accounts and log in securely.
- User Management: Manage profiles, preferences, and roles.
- Authentication: Secure access with JWT-based authentication.
- Integration: Works with other services like Ansopedia Studio API to manage user permissions.
- build: Transpiles TypeScript and fixes path aliases.
- local: Starts the server with
NODE_ENV=local. - local:dev: Starts the server with
NODE_ENV=development. - local:prod: Starts the server with
NODE_ENV=productionand runs the production build. - generate-keys: Generates RSA keys for JWT authentication.
- generate-env: Generates
.envfiles from templates. - migrate: Runs migrations for the current
NODE_ENV(used internally by other migrate scripts). - migrate:local: Runs migrations for
NODE_ENV=local. - migrate:prod: Runs migrations for
NODE_ENV=production. - setup: Generates environment files and RSA keys.
- lint / lint:fix: Lint code and optionally fix issues.
- prettier:check / prettier:fix: Check or fix formatting.
- prepare: Runs Husky hooks.
- prod: Builds the app and starts in production.
- start: Runs the built app.
- test / test:coverage: Run tests, optionally with coverage.
Follow these steps to set up the project:
-
Clone the repository
git clone https://github.com/ansopedia/user-service.git cd user-service -
Install dependencies
pnpm install
-
Automated setup
pnpm run setup
This:
- Creates
.envfiles for local, development, and test. - Generates RSA keys.
- Injects keys into
.envfiles.
- Creates
-
Run database migrations
pnpm migrate:local
Or for production:
pnpm migrate:prod
-
Verify setup
pnpm test -
Start development server
pnpm localOr:
pnpm local:dev
-
Setup:
pnpm setup→ Generate env files & RSA keys.pnpm generate-env→ Env files only.pnpm generate-keys→ RSA keys only.
-
Migrations:
pnpm migrate→ Run for currentNODE_ENV.pnpm migrate:local→ Run for local.pnpm migrate:prod→ Run for production.
-
Development:
pnpm local→ Local environment.pnpm local:dev→ Development environment.pnpm local:prod→ Production environment (locally).
-
Production:
pnpm build→ Build app.pnpm prod→ Build & run.pnpm start→ Start built app.
-
Testing:
pnpm testpnpm test:coverage
-
Code Quality:
pnpm lintpnpm lint:fixpnpm prettier:checkpnpm prettier:fix
The service uses multiple environment configs:
.env.local→ Local development.env.development→ Dev server deployment.env.test→ Testing.env.production→ Production (manually or via secrets in deployment)
These are loaded based on NODE_ENV.
Never commit .env files with real credentials.
You can build and run the Ansopedia User Service using Docker. This ensures a consistent environment and makes deployment easier.
The easiest way to run the service with its dependencies (MongoDB and Redis) is using Docker Compose.
-
Ensure you have a
.envfile with the required environment variables (see Environment Configuration section). -
Run the services:
docker-compose up --build
Or to run in detached mode:
docker-compose up -d --build
This will:
- Build the user service image (including copying the GeoIP database).
- Start MongoDB and Redis containers.
- Start the user service container connected to the databases.
- Expose the service on port 3000.
-
To stop the services:
docker-compose down
If you prefer to run the container standalone (e.g., connecting to external databases), follow these steps:
docker build -t ansopedia-user-service .You should pass environment variables like NODE_ENV and any secrets (database URLs, API keys) at runtime.
Example using environment variables inline:
docker run -p 3000:3000 --env-file .env ansopedia-user-serviceAlternatively, use an .env file to manage environment variables:
-
Create a
.envfile with the needed variables:NODE_ENV=production DATABASE_URI=your_DATABASE_URI JWT_PRIVATE_KEY="your_jwt_private_key"
-
Run the container with the env file:
docker run -d -p 3000:3000 --env-file .env ansopedia-user-service
- The Dockerfile already sets
NODE_ENV=productionby default, but explicitly passing it at runtime is a good practice for clarity. - When using Docker Compose, ensure your
.envfile contains the correct database connection strings (e.g.,mongodb://mongodb:27017/yourdbfor the MongoDB service). - Never commit secrets or private keys into your Docker image or source control.
- Use Docker secrets or your cloud provider’s secret management for production deployments.
- The container exposes port
3000by default; you can map it to any host port you prefer. docker build -t ansopedia-user-service .
- Never commit RSA keys.
- Use a secure key vault in production.
- Rotate keys periodically.
- Restrict access to private keys.
See CONTRIBUTING.md.
This project is licensed under the terms in LICENSE.