Skip to content

Commit 440b6a3

Browse files
committed
use environment variables instead of local config file
1 parent 732c3ba commit 440b6a3

20 files changed

Lines changed: 293 additions & 259 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
.DS_Store
22
node_modules
33
.env
4+
.env.local
45
**/auth0-variables.js
6+
**/build
7+
**/dist
58
npm-debug.log
69
yarn-error.log
710
.editorconfig

Sample-01/.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
AUTH0_DOMAIN={yourDomain}
2+
AUTH0_CLIENT_ID={yourClientId}
3+
# AUTH0_AUDIENCE={yourApiIdentifier}
4+
5+
# APP_BASE_URL=https://your-app.com
6+
# API_BASE_URL=https://your-api-server.com

Sample-01/Dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ RUN npm install
1616

1717
COPY . .
1818

19-
RUN npm run build
19+
ARG AUTH0_DOMAIN
20+
ARG AUTH0_CLIENT_ID
21+
ARG AUTH0_AUDIENCE
22+
ARG API_BASE_URL
23+
24+
RUN AUTH0_DOMAIN=${AUTH0_DOMAIN} \
25+
AUTH0_CLIENT_ID=${AUTH0_CLIENT_ID} \
26+
AUTH0_AUDIENCE=${AUTH0_AUDIENCE} \
27+
API_BASE_URL=${API_BASE_URL} \
28+
npm run build
2029

2130
# ---------------
2231

@@ -35,7 +44,6 @@ COPY --from=build /app/package.json .
3544
RUN npm install --omit=dev
3645

3746
COPY --from=build /app/dist ./dist
38-
COPY --from=build /app/src/auth_config.json ./src/auth_config.json
3947
COPY --from=build /app/server.js .
4048
COPY --from=build /app/api-server.js .
4149

Sample-01/README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Auth0 React SDK Sample Application
22

3-
This sample demonstrates the integration of [Auth0 React SDK](https://github.com/auth0/auth0-react) into a React application created using [create-react-app](https://reactjs.org/docs/create-a-new-react-app.html). The sample is a companion to the [Auth0 React SDK Quickstart](https://auth0.com/docs/quickstart/spa/react).
3+
This sample demonstrates the integration of [Auth0 React SDK](https://github.com/auth0/auth0-react) into a React application created using [Vite](https://reactjs.org/docs/create-a-new-react-app.html). The sample is a companion to the [Auth0 React SDK Quickstart](https://auth0.com/docs/quickstart/spa/react).
44

55
This sample demonstrates the following use cases:
66

@@ -22,27 +22,26 @@ npm install
2222

2323
### Create an API
2424

25-
For the ["call an API"](https://auth0.com/docs/quickstart/spa/react/02-calling-an-api) page to work, you will need to [create an API](https://auth0.com/docs/apis) using the [management dashboard](https://manage.auth0.com/#/apis). This will give you an API identifier that you can use in the `audience` configuration field below.
25+
For the ["call an API"](https://auth0.com/docs/quickstart/spa/react/02-calling-an-api) page to work, you will need to [create an API](https://auth0.com/docs/apis) using the [management dashboard](https://manage.auth0.com/#/apis). This will give you an API identifier that you can use as the `AUTH0_AUDIENCE` environment variable below.
2626

27-
If you do not wish to use an API or observe the API call working, you should not specify the `audience` value in the next step. Otherwise, you will receive a "Service not found" error when trying to authenticate.
27+
If you do not wish to use an API or observe the API call working, you should not specify the `AUTH0_AUDIENCE` environment variable in the next step. Otherwise, you will receive a "Service not found" error when trying to authenticate.
2828

2929
### Configure credentials
3030

3131
The project needs to be configured with your Auth0 domain and client ID in order for the authentication flow to work.
3232

33-
To do this, first copy `src/auth_config.json.example` into a new file in the same folder called `src/auth_config.json`, and replace the values with your own Auth0 application credentials, and optionally the base URLs of your application and API:
33+
To do this, first copy `.env.example` into a new file in the same folder called `.env.local`, and replace the values with your own Auth0 application credentials, and optionally the base URLs of your application and API:
3434

35-
```json
36-
{
37-
"domain": "{YOUR AUTH0 DOMAIN}",
38-
"clientId": "{YOUR AUTH0 CLIENT ID}",
39-
"audience": "{YOUR AUTH0 API_IDENTIFIER}",
40-
"appOrigin": "{OPTIONAL: THE BASE URL OF YOUR APPLICATION (default: http://localhost:3000)}",
41-
"apiOrigin": "{OPTIONAL: THE BASE URL OF YOUR API (default: http://localhost:3001)}"
42-
}
35+
```bash
36+
AUTH0_DOMAIN=your-tenant.us.auth0.com
37+
AUTH0_CLIENT_ID=your-client-id
38+
# AUTH0_AUDIENCE=your-api-identifier # optional, only needed for the External API feature
39+
40+
# APP_BASE_URL=https://your-app.com # optional, defaults work fine for local development
41+
# API_BASE_URL=https://your-api-server.com # optional, defaults work fine for local development
4342
```
4443

45-
**Note**: Do not specify a value for `audience` here if you do not wish to use the API part of the sample.
44+
**Note**: Do not specify a value for `AUTH0_AUDIENCE` here if you do not wish to use the API part of the sample.
4645

4746
## Run the sample
4847

Sample-01/api-server.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1+
require('dotenv').config({ path: '.env.local' })
2+
13
const express = require("express");
24
const cors = require("cors");
35
const morgan = require("morgan");
46
const helmet = require("helmet");
57
const { auth } = require("express-oauth2-jwt-bearer");
6-
const authConfig = require("./src/auth_config.json");
78

89
const app = express();
910

1011
const port = process.env.API_PORT || 3001;
1112
const appPort = process.env.SERVER_PORT || 3000;
12-
const appOrigin = authConfig.appOrigin || `http://localhost:${appPort}`;
13+
const appOrigin = process.env.APP_BASE_URL || `http://localhost:${appPort}`;
14+
const domain = process.env.AUTH0_DOMAIN;
15+
const audience = process.env.AUTH0_AUDIENCE;
1316

1417
if (
15-
!authConfig.domain ||
16-
!authConfig.audience ||
17-
authConfig.audience === "{API_IDENTIFIER}"
18+
!domain ||
19+
!audience ||
20+
audience === "{yourApiIdentifier}"
1821
) {
1922
console.log(
20-
"Exiting: Please make sure that auth_config.json is in place and populated with valid domain and audience values"
23+
"Exiting API Server: Please make sure AUTH0_DOMAIN and AUTH0_AUDIENCE environment variables are set"
2124
);
2225

2326
process.exit();
@@ -28,8 +31,8 @@ app.use(helmet());
2831
app.use(cors({ origin: appOrigin }));
2932

3033
const checkJwt = auth({
31-
audience: authConfig.audience,
32-
issuerBaseURL: `https://${authConfig.domain}/`,
34+
audience,
35+
issuerBaseURL: `https://${domain}/`,
3336
algorithms: ["RS256"],
3437
});
3538

Sample-01/exec.ps1

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
docker build --rm -t auth0-react-01-login .
2-
docker run --init -p 3000:3000 -p 3001:3001 -it auth0-react-01-login
1+
if (-not (Test-Path .env.local)) {
2+
Write-Error "Error: .env.local not found. Copy .env.example to .env.local and fill in your credentials."
3+
exit 1
4+
}
5+
6+
$envVars = @{}
7+
Get-Content .env.local | Where-Object { $_ -notmatch '^#' -and $_ -notmatch '^\s*$' } | ForEach-Object {
8+
$parts = $_ -split '=', 2
9+
$envVars[$parts[0].Trim()] = $parts[1].Trim()
10+
}
11+
12+
docker build `
13+
--build-arg "AUTH0_DOMAIN=$($envVars['AUTH0_DOMAIN'])" `
14+
--build-arg "AUTH0_CLIENT_ID=$($envVars['AUTH0_CLIENT_ID'])" `
15+
--build-arg "AUTH0_AUDIENCE=$($envVars['AUTH0_AUDIENCE'])" `
16+
--build-arg "API_BASE_URL=$($envVars['API_BASE_URL'])" `
17+
-t auth0-react-01-login .
18+
19+
docker run --init -p 3000:3000 -p 3001:3001 --env-file .env.local -it auth0-react-01-login

Sample-01/exec.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
#!/usr/bin/env bash
2-
docker build -t auth0-react-02-calling-an-api .
3-
docker run --init -p 3000:3000 -p 3001:3001 -it auth0-react-02-calling-an-api
2+
if [ ! -f .env.local ]; then
3+
echo "Error: .env.local not found. Copy .env.example to .env.local and fill in your credentials."
4+
exit 1
5+
fi
6+
7+
export $(grep -v '^#' .env.local | grep -v '^$' | xargs)
8+
9+
docker build \
10+
--build-arg AUTH0_DOMAIN="$AUTH0_DOMAIN" \
11+
--build-arg AUTH0_CLIENT_ID="$AUTH0_CLIENT_ID" \
12+
--build-arg AUTH0_AUDIENCE="$AUTH0_AUDIENCE" \
13+
--build-arg API_BASE_URL="$API_BASE_URL" \
14+
-t auth0-react-02-calling-an-api .
15+
16+
docker run --init -p 3000:3000 -p 3001:3001 --env-file .env.local -it auth0-react-02-calling-an-api

0 commit comments

Comments
 (0)