Overview
The Portal Self-Service Backend requires several environment variables to configure database connections, Azure AD authentication, server settings, and CORS policies.Environment Variables
Server Configuration
Server Port
3000 if not specified.Database Configuration
All database variables are required for the application to start. See the Database Setup guide for detailed configuration.
| Variable | Description | Required |
|---|---|---|
DB_USER | Database authentication username | ✅ Yes |
DB_PASSWORD | Database authentication password | ✅ Yes |
DB_SERVER | Database server hostname or IP address | ✅ Yes |
DB_DATABASE | Target database name | ✅ Yes |
DB_PORT | SQL Server port (typically 1433) | ✅ Yes |
Azure AD Authentication
The application uses Azure AD for authentication via JWT bearer tokens:| Variable | Description | Example |
|---|---|---|
AZURE_ISSUER_BASE_URL | Azure AD token issuer URL including tenant ID | https://login.microsoftonline.com/{tenant-id}/v2.0 |
AZURE_AUDIENCE | Application ID URI of your backend API registered in Azure | api://your-backend-api-id |
How It’s Used
Authentication Middleware (src/middleware/authMiddleware.js):
src/config/socket.js):
Get Your Tenant ID
- Go to Azure Portal → Azure Active Directory
- Copy the Directory (tenant) ID
- Use format:
https://login.microsoftonline.com/{tenant-id}/v2.0
Get Your API Application ID
- Go to Azure Portal → App Registrations
- Select your backend API registration
- Copy the Application ID URI (format:
api://your-app-id)
Frontend Configuration
Frontend URL
- CORS configuration (API requests)
- WebSocket CORS (Socket.io connections)
The default CORS origin in
src/app.js is http://localhost:5173. Update both the FRONTEND_URL variable and the hardcoded corsOptions for consistency.src/app.js:
File Upload Configuration
Base URL
src/controllers/uploadController.js:
Complete .env Template
Create a.env file in the root of your project with the following structure:
Environment Variable Reference Table
| Variable | Type | Required | Default | Description |
|---|---|---|---|---|
PORT | number | No | 3000 | HTTP server port |
DB_USER | string | Yes | - | Database username |
DB_PASSWORD | string | Yes | - | Database password |
DB_SERVER | string | Yes | - | Database server hostname |
DB_DATABASE | string | Yes | - | Database name |
DB_PORT | number | Yes | - | Database port (typically 1433) |
AZURE_ISSUER_BASE_URL | string | Yes | - | Azure AD issuer URL with tenant ID |
AZURE_AUDIENCE | string | Yes | - | API application ID URI |
FRONTEND_URL | string | No | http://localhost:5173 | Frontend application URL for CORS |
BASE_URL | string | No | http://localhost:3000 | Backend base URL for file uploads |
Security Best Practices
Never Commit .env
Add
.env to your .gitignore file to prevent exposing secrets in version control..gitignore
Use Strong Passwords
Database passwords should be:
- At least 12 characters
- Include uppercase, lowercase, numbers, and symbols
- Different from development passwords
Rotate Credentials
Periodically rotate:
- Database passwords
- Azure AD client secrets
- Any API keys
Use Azure Key Vault
For production deployments, consider using Azure Key Vault or similar secret management services instead of plain
.env files.Validation
To verify your environment configuration:Troubleshooting
Missing Environment Variables
If you see errors about undefined environment variables:- Ensure
.envfile exists in the project root - Verify
dotenvis loaded inserver.js: - Check that variable names match exactly (case-sensitive)
Azure AD Authentication Errors
Invalid Token
Symptoms:
401 Unauthorized or “Token inválido” errorsSolutions:- Verify
AZURE_ISSUER_BASE_URLincludes your correct tenant ID - Ensure
AZURE_AUDIENCEmatches the Application ID URI in Azure - Check that the frontend is requesting tokens with the correct scope
- Verify token hasn’t expired
CORS Errors
CORS Policy Blocked
Symptoms: Browser console shows CORS policy errorsSolutions:
- Verify
FRONTEND_URLmatches your frontend application URL - Update
corsOptions.origininsrc/app.jsto useprocess.env.FRONTEND_URL - Ensure frontend makes requests with
credentials: 'include'if using cookies
Next Steps
Database Setup
Configure your MSSQL database connection
Deployment
Deploy your application to production