Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Development API Configuration
REACT_APP_API_URL=https://api.policyengine.org
REACT_APP_API_AUDIENCE=https://api.policyengine.org/

# Development mode flag
REACT_APP_DEBUG=true
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# API Configuration
REACT_APP_API_URL=https://api.policyengine.org
REACT_APP_API_AUDIENCE=https://api.policyengine.org/

# Development mode
REACT_APP_DEBUG=true

# For local development, uncomment these and use appropriate URLs
# REACT_APP_API_URL=http://localhost:5000
# REACT_APP_API_AUDIENCE=http://localhost:5000/
6 changes: 6 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Production API Configuration
REACT_APP_API_URL=https://api.policyengine.org
REACT_APP_API_AUDIENCE=https://api.policyengine.org/

# Debug mode should be off in production
REACT_APP_DEBUG=false
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ We recommend that you use the latest Node version 19. To easily manage your node
nvm install 19 && nvm use 19
```

# Environment Variables

The application uses environment variables to configure API endpoints and other settings. These are defined in the following files:

- `.env.development` - Used during local development
- `.env.production` - Used in production builds

You can customize your local development setup by modifying `.env.development` or creating a `.env.local` file which will override other environment files.

Available environment variables:

| Variable | Description | Default |
|----------|-------------|---------|
| REACT_APP_API_URL | Base URL for API requests | https://api.policyengine.org |
| REACT_APP_API_AUDIENCE | Auth0 audience for API | https://api.policyengine.org/ |
| REACT_APP_DEBUG | Enable debug mode | true (dev) / false (prod) |

For local development with a backend running on your machine, you might want to use:
```
REACT_APP_API_URL=http://localhost:5000
REACT_APP_API_AUDIENCE=http://localhost:5000/
REACT_APP_DEBUG=true
```

# Contributing

## Choosing an Issue
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"moment": "^2.30.1",
"openai": "^4.91.0",
"plotly.js": "^2.35.3",
"policyengine-app": "file:",
"react": "^18.3.1",
"react-bootstrap": "^2.10.9",
"react-detect-print": "^0.1.2",
Expand Down
9 changes: 4 additions & 5 deletions src/api/call.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useCallback } from "react";
import { buildParameterTree } from "./parameters";
import { buildVariableTree, getTreeLeavesInOrder } from "./variables";
import { API_URL } from "../config";
import { wrappedJsonStringify, wrappedResponseJson } from "../data/wrappedJson";
import { useAuthenticatedFetch } from "../hooks/useAuthenticatedFetch";

const POLICYENGINE_API = "https://api.policyengine.org";
import { buildParameterTree } from "./parameters";
import { buildVariableTree, getTreeLeavesInOrder } from "./variables";

/**
* returns an api call function that can be used to make requests
Expand Down Expand Up @@ -47,7 +46,7 @@ export function apiCall(
secondAttempt = false,
fetchMethod = fetch,
) {
return fetchMethod(POLICYENGINE_API + path, {
return fetchMethod(API_URL + path, {
Copy link

Copilot AI May 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider ensuring consistent slash usage when concatenating API_URL with path, as the API_URL and API_AUDIENCE values use different trailing slash conventions. A utility function or normalization could prevent potential URL concatenation issues.

Copilot uses AI. Check for mistakes.
method: method || (body ? "POST" : "GET"),
headers: {
"Content-Type": "application/json",
Expand Down
10 changes: 4 additions & 6 deletions src/auth/Auth0ProviderWithNavigate.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { useNavigate } from "react-router-dom";
import { Auth0Provider } from "@auth0/auth0-react";
import { AUTH0_CLIENT_ID, AUTH0_DOMAIN } from "./authUtils";
import { useNavigate } from "react-router-dom";
import { API_AUDIENCE, AUTH0_CLIENT_ID, AUTH0_DOMAIN, AUTH0_REDIRECT_URI } from "../config";

export default function Auth0ProviderWithNavigate({ children }) {
const navigate = useNavigate();

const domain = AUTH0_DOMAIN;
const clientId = AUTH0_CLIENT_ID;

const redirectUri = process.env.REACT_APP_DEBUG
? "http://localhost:3000/callback"
: "https://policyengine.org/callback";
const redirectUri = AUTH0_REDIRECT_URI;

const onRedirectCallback = (appState) => {
navigate(appState?.returnTo || window.location.pathname);
Expand All @@ -26,7 +24,7 @@ export default function Auth0ProviderWithNavigate({ children }) {
clientId={clientId}
authorizationParams={{
redirect_uri: redirectUri,
audience: "https://api.policyengine.org/",
audience: API_AUDIENCE,
}}
onRedirectCallback={onRedirectCallback}
useRefreshTokens={true}
Expand Down
3 changes: 0 additions & 3 deletions src/auth/authUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// The below are two public values vital to auth0 local setup
export const AUTH0_DOMAIN = "policyengine.uk.auth0.com";
export const AUTH0_CLIENT_ID = "jbAXjeFRxGpGRxkedjt2wRLHJd26bwDS";

/**
* Provide login options for use with auth0
Expand Down
17 changes: 17 additions & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Configuration values for the application
* These can be overridden by environment variables
*/

// API URLs
export const API_URL = process.env.REACT_APP_API_URL || "https://api.policyengine.org";
export const API_AUDIENCE = process.env.REACT_APP_API_AUDIENCE || "https://api.policyengine.org/";

// Auth configuration (moved from authUtils.js for centralization)
export const AUTH0_DOMAIN = "policyengine.uk.auth0.com";
export const AUTH0_CLIENT_ID = "jbAXjeFRxGpGRxkedjt2wRLHJd26bwDS";

// Redirect URL for Auth0
export const AUTH0_REDIRECT_URI = process.env.REACT_APP_DEBUG
? "http://localhost:3000/callback"
: "https://policyengine.org/callback";
3 changes: 2 additions & 1 deletion src/hooks/useAuthenticatedFetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useAuth0 } from "@auth0/auth0-react";
import { useCallback } from "react";
import { API_AUDIENCE } from "../config";

/**
* Get an 'authenticatedFetch' function which, if the user is logged in,
Expand All @@ -18,7 +19,7 @@ export function useAuthenticatedFetch() {
try {
//as per https://auth0.com/docs/quickstart/spa/react/02-calling-an-api
const accessToken = await getAccessTokenSilently({
audience: "https://api.policyengine.org/",
audience: API_AUDIENCE,
});
headers["Authorization"] = `Bearer ${accessToken}`;
} catch (error) {
Expand Down
Loading