Skip to content

Commit b580071

Browse files
authored
Modernize sample app with React 19, Vite, npm and dependency updates (#392)
* upgrade react major version and migrate to vite * migrate from reactstrap to react-bootstrap * upgrade nodemon * update dockerfile * change build folder to vite default * use environment variables instead of local config file * fix circleci error * address feedback * update circleci config docker version
1 parent 428e51c commit b580071

39 files changed

Lines changed: 11658 additions & 10401 deletions

.circleci/config.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version: 2.1
99
orbs:
1010
node: circleci/node@5.0.2
1111
docker: circleci/docker@2.0.3
12-
12+
1313
jobs:
1414
checkout:
1515
docker:
@@ -26,37 +26,31 @@ jobs:
2626
docker:
2727
- image: cimg/node:lts
2828
environment:
29-
- AUTH0_CFG: Sample-01/src/auth_config.json
3029
- SAMPLE_PATH: Sample-01
3130
steps:
32-
- node/install:
33-
install-yarn: true
31+
- node/install
3432
- attach_workspace:
3533
at: ~/
36-
- run:
37-
name: Replace Auth0 test credentials
38-
command: |
39-
echo "{ \"domain\": \"$AUTH0_TEST_DOMAIN\", \"clientId\": \"$AUTH0_TEST_CLIENT_ID\", \"audience\": \"$AUTH0_TEST_API_IDENTIFIER\" }" > $AUTH0_CFG
4034
- setup_remote_docker:
41-
version: 19.03.13
35+
version: default
4236
- run:
4337
name: Build pull request
4438
command: |
45-
docker build -t $CIRCLE_JOB ./$SAMPLE_PATH
39+
docker build --build-arg AUTH0_DOMAIN=$AUTH0_TEST_DOMAIN --build-arg AUTH0_CLIENT_ID=$AUTH0_TEST_CLIENT_ID --build-arg AUTH0_AUDIENCE=$AUTH0_TEST_API_IDENTIFIER -t $CIRCLE_JOB ./$SAMPLE_PATH
4640
docker run -d -p 3000:3000 --name $CIRCLE_SHA1 $CIRCLE_JOB
4741
- run:
4842
name: Wait for app to be available
4943
command: |
5044
sleep 10
5145
docker run --network host --rm appropriate/curl --retry 8 --retry-connrefused -v localhost:3000
5246
- node/install-packages:
53-
pkg-manager: yarn
47+
pkg-manager: npm
5448
app-dir: Sample-01
5549
- run:
5650
name: Run unit tests
5751
command: |
5852
cd Sample-01
59-
yarn test
53+
npm test
6054
- run:
6155
name: Run integration tests
6256
command: |

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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
9+
yarn-error.log
610
.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: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:lts-alpine3.14 as build
1+
FROM node:lts-alpine3.20 AS build
22

33
RUN apk update && \
44
apk upgrade && \
@@ -10,19 +10,28 @@ WORKDIR /app
1010

1111
COPY package.json .
1212

13-
RUN npm install -g --force npm@latest typescript@latest yarn@latest
13+
RUN npm install -g --force npm@latest
1414

15-
RUN yarn install
15+
RUN npm install
1616

1717
COPY . .
1818

19-
RUN yarn 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

23-
FROM node:lts-alpine3.14
32+
FROM node:lts-alpine3.20
2433

25-
RUN mkdir -p /app/build
34+
RUN mkdir -p /app/dist
2635

2736
RUN apk update && \
2837
apk upgrade && \
@@ -32,10 +41,9 @@ WORKDIR /app
3241

3342
COPY --from=build /app/package.json .
3443

35-
RUN yarn install --production
44+
RUN npm install --omit=dev
3645

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

@@ -44,6 +52,6 @@ EXPOSE 3001
4452

4553
ENV SERVER_PORT=3000
4654
ENV API_PORT=3001
47-
ENV NODE_ENV production
55+
ENV NODE_ENV=production
4856

49-
CMD ["yarn", "prod"]
57+
CMD ["npm", "run", "prod"]

Sample-01/README.md

Lines changed: 23 additions & 24 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://vite.dev). 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

@@ -12,37 +12,36 @@ This sample demonstrates the following use cases:
1212

1313
## Project setup
1414

15-
Use `yarn` to install the project dependencies:
15+
Use `npm` to install the project dependencies:
1616

1717
```bash
18-
yarn install
18+
npm install
1919
```
2020

2121
## Configuration
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

@@ -51,15 +50,15 @@ To do this, first copy `src/auth_config.json.example` into a new file in the sam
5150
This compiles and serves the React app and starts the backend API server on port 3001.
5251

5352
```bash
54-
yarn run dev
53+
npm run dev
5554
```
5655

5756
## Deployment
5857

5958
### Compiles and minifies for production
6059

6160
```bash
62-
yarn run build
61+
npm run build
6362
```
6463

6564
### Docker build
@@ -69,7 +68,7 @@ To build and run the Docker image, run `exec.sh`, or `exec.ps1` on Windows.
6968
### Run your tests
7069

7170
```bash
72-
yarn run test
71+
npm test
7372
```
7473

7574
## Frequently Asked Questions
@@ -80,12 +79,12 @@ If you're having issues running the sample applications, including issues such a
8079

8180
Auth0 helps you to:
8281

83-
* Add authentication with [multiple sources](https://auth0.com/docs/identityproviders), either social identity providers such as **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce** (amongst others), or enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS, or any SAML Identity Provider**.
84-
* Add authentication through more traditional **[username/password databases](https://auth0.com/docs/connections/database/custom-db)**.
85-
* Add support for **[linking different user accounts](https://auth0.com/docs/users/user-account-linking)** with the same user.
86-
* Support for generating signed [JSON Web Tokens](https://auth0.com/docs/tokens/json-web-tokens) to call your APIs and **flow the user identity** securely.
87-
* Analytics of how, when, and where users are logging in.
88-
* Pull data from other sources and add it to the user profile through [JavaScript rules](https://auth0.com/docs/rules).
82+
- Add authentication with [multiple sources](https://auth0.com/docs/identityproviders), either social identity providers such as **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce** (amongst others), or enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS, or any SAML Identity Provider**.
83+
- Add authentication through more traditional **[username/password databases](https://auth0.com/docs/connections/database/custom-db)**.
84+
- Add support for **[linking different user accounts](https://auth0.com/docs/users/user-account-linking)** with the same user.
85+
- Support for generating signed [JSON Web Tokens](https://auth0.com/docs/tokens/json-web-tokens) to call your APIs and **flow the user identity** securely.
86+
- Analytics of how, when, and where users are logging in.
87+
- Pull data from other sources and add it to the user profile through [JavaScript rules](https://auth0.com/docs/rules).
8988

9089
## Create a Free Auth0 Account
9190

Sample-01/__mocks__/fileMock.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Exports empty object since Jest can't interpret CSS syntax
2+
3+
module.exports = {}

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/babel.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', { targets: { node: 'current' } }],
4+
['@babel/preset-react', { runtime: 'automatic' }],
5+
],
6+
}

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

100644100755
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
AUTH0_DOMAIN=$(grep '^AUTH0_DOMAIN=' .env.local | cut -d '=' -f2-)
8+
AUTH0_CLIENT_ID=$(grep '^AUTH0_CLIENT_ID=' .env.local | cut -d '=' -f2-)
9+
AUTH0_AUDIENCE=$(grep '^AUTH0_AUDIENCE=' .env.local | cut -d '=' -f2-)
10+
API_BASE_URL=$(grep '^API_BASE_URL=' .env.local | cut -d '=' -f2-)
11+
12+
docker build \
13+
--build-arg AUTH0_DOMAIN="$AUTH0_DOMAIN" \
14+
--build-arg AUTH0_CLIENT_ID="$AUTH0_CLIENT_ID" \
15+
--build-arg AUTH0_AUDIENCE="$AUTH0_AUDIENCE" \
16+
--build-arg API_BASE_URL="$API_BASE_URL" \
17+
-t auth0-react-02-calling-an-api .
18+
19+
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)