Skip to content

Commit 764c7bf

Browse files
adamjmcgrathSteve Hobbs
andauthored
[SDK-1192] Remove proxy, separate app and api servers (#186)
* Remove proxy, separate app and api servers * expose api port for windows * Make app and api base uris configurable * yarn upgrade * Update 02-Calling-an-API/README.md Co-Authored-By: Steve Hobbs <[email protected]> * Whitespace change to trigger rebuild Co-authored-by: Steve Hobbs <[email protected]>
1 parent 5368f91 commit 764c7bf

File tree

11 files changed

+1341
-987
lines changed

11 files changed

+1341
-987
lines changed

01-Login/server.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@ const port = process.env.SERVER_PORT || 3000;
99
app.use(morgan("dev"));
1010
app.use(express.static(join(__dirname, "build")));
1111

12-
app.use((_, res) => {
13-
res.sendFile(join(__dirname, "build", "index.html"));
14-
});
15-
1612
app.listen(port, () => console.log(`Listening on port ${port}`));

01-Login/yarn.lock

Lines changed: 628 additions & 456 deletions
Large diffs are not rendered by default.

02-Calling-an-API/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ RUN yarn install --production
3232
COPY --from=build /app/build ./build
3333
COPY --from=build /app/src/auth_config.json ./src/auth_config.json
3434
COPY --from=build /app/server.js .
35+
COPY --from=build /app/api-server.js .
3536

3637
EXPOSE 3000
38+
EXPOSE 3001
39+
3740
ENV SERVER_PORT=3000
41+
ENV API_PORT=3001
3842
ENV NODE_ENV production
3943

40-
CMD ["node", "server.js"]
44+
CMD ["yarn", "prod"]

02-Calling-an-API/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ yarn install
1818

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

21-
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:
21+
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:
2222

2323
```json
2424
{
2525
"domain": "{YOUR AUTH0 DOMAIN}",
2626
"clientId": "{YOUR AUTH0 CLIENT ID}",
27-
"audience": "{YOUR AUTH0 API_IDENTIFIER}"
27+
"audience": "{YOUR AUTH0 API_IDENTIFIER}",
28+
"appOrigin": "{OPTIONAL: THE BASE URL OF YOUR APPLICATION (default: http://localhost:3000)}",
29+
"apiOrigin": "{OPTIONAL: THE BASE URL OF YOUR API (default: http://localhost:3001)}"
2830
}
2931
```
3032

3133
### Compiles and hot-reloads for development
3234

33-
This compiles and serves the React app, and starts the backend API server on port 3001. Calls to `http://localhost:3000/api/*` routes will be proxied through to the backend:
35+
This compiles and serves the React app and starts the backend API server on port 3001.
3436

3537
```bash
3638
npm run dev

02-Calling-an-API/api-server.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const express = require("express");
2+
const cors = require("cors");
3+
const morgan = require("morgan");
4+
const helmet = require("helmet");
5+
const jwt = require("express-jwt");
6+
const jwksRsa = require("jwks-rsa");
7+
const authConfig = require("./src/auth_config.json");
8+
9+
const app = express();
10+
11+
const port = process.env.API_PORT || 3001;
12+
const appPort = process.env.SERVER_PORT || 3000;
13+
const appOrigin = authConfig.appOrigin || `http://localhost:${appPort}`;
14+
15+
if (!authConfig.domain || !authConfig.audience) {
16+
throw new Error(
17+
"Please make sure that auth_config.json is in place and populated"
18+
);
19+
}
20+
21+
app.use(morgan("dev"));
22+
app.use(helmet());
23+
app.use(cors({ origin: appOrigin }));
24+
25+
const checkJwt = jwt({
26+
secret: jwksRsa.expressJwtSecret({
27+
cache: true,
28+
rateLimit: true,
29+
jwksRequestsPerMinute: 5,
30+
jwksUri: `https://${authConfig.domain}/.well-known/jwks.json`
31+
}),
32+
33+
audience: authConfig.audience,
34+
issuer: `https://${authConfig.domain}/`,
35+
algorithm: ["RS256"]
36+
});
37+
38+
app.get("/api/external", checkJwt, (req, res) => {
39+
res.send({
40+
msg: "Your access token was successfully validated!"
41+
});
42+
});
43+
44+
app.listen(port, () => console.log(`API Server listening on port ${port}`));

02-Calling-an-API/exec.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
docker build --rm -t auth0-react-02-calling-an-api .
2-
docker run --init -p 3000:3000 -it auth0-react-02-calling-an-api
2+
docker run --init -p 3000:3000 -p 3001:3001 -it auth0-react-02-calling-an-api

02-Calling-an-API/exec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22
docker build -t auth0-react-02-calling-an-api .
3-
docker run --init -p 3000:3000 -it auth0-react-02-calling-an-api
3+
docker run --init -p 3000:3000 -p 3001:3001 -it auth0-react-02-calling-an-api

02-Calling-an-API/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"start": "npm-run-all --parallel spa server",
6+
"start": "npm-run-all --parallel spa api-server",
77
"build": "react-scripts build",
88
"test": "react-scripts test",
99
"eject": "react-scripts eject",
1010
"spa": "react-scripts start",
1111
"server": "node server.js",
1212
"server:dev": "nodemon server.js",
13-
"dev": "npm-run-all --parallel spa server:dev"
13+
"api-server": "node api-server.js",
14+
"api-server:dev": "nodemon api-server.js",
15+
"dev": "npm-run-all --parallel spa api-server:dev",
16+
"prod": "npm-run-all --parallel server api-server"
1417
},
1518
"dependencies": {
1619
"@auth0/auth0-spa-js": "^1.0.2",
1720
"@fortawesome/fontawesome-svg-core": "^1.2.17",
1821
"@fortawesome/free-solid-svg-icons": "^5.8.1",
1922
"@fortawesome/react-fontawesome": "^0.1.4",
23+
"cors": "^2.8.5",
2024
"express": "^4.16.4",
2125
"express-jwt": "^5.3.1",
2226
"helmet": "^3.18.0",
@@ -47,6 +51,5 @@
4751
"last 1 firefox version",
4852
"last 1 safari version"
4953
]
50-
},
51-
"proxy": "http://localhost:3001"
54+
}
5255
}

02-Calling-an-API/server.js

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,14 @@
11
const express = require("express");
22
const morgan = require("morgan");
33
const helmet = require("helmet");
4-
const jwt = require("express-jwt");
5-
const jwksRsa = require("jwks-rsa");
64
const { join } = require("path");
7-
const authConfig = require("./src/auth_config.json");
85

96
const app = express();
107

11-
const port = process.env.SERVER_PORT || 3001;
12-
13-
if (!authConfig.domain || !authConfig.audience) {
14-
throw new Error(
15-
"Please make sure that auth_config.json is in place and populated"
16-
);
17-
}
8+
const port = process.env.SERVER_PORT || 3000;
189

1910
app.use(morgan("dev"));
2011
app.use(helmet());
2112
app.use(express.static(join(__dirname, "build")));
2213

23-
const checkJwt = jwt({
24-
secret: jwksRsa.expressJwtSecret({
25-
cache: true,
26-
rateLimit: true,
27-
jwksRequestsPerMinute: 5,
28-
jwksUri: `https://${authConfig.domain}/.well-known/jwks.json`
29-
}),
30-
31-
audience: authConfig.audience,
32-
issuer: `https://${authConfig.domain}/`,
33-
algorithm: ["RS256"]
34-
});
35-
36-
app.get("/api/external", checkJwt, (req, res) => {
37-
res.send({
38-
msg: "Your access token was successfully validated!"
39-
});
40-
});
41-
42-
app.use((_, res) => {
43-
res.sendFile(join(__dirname, "build", "index.html"));
44-
});
45-
4614
app.listen(port, () => console.log(`Server listening on port ${port}`));

02-Calling-an-API/src/views/ExternalApi.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import React, { useState } from "react";
22
import { Button } from "reactstrap";
33
import Highlight from "../components/Highlight";
44
import { useAuth0 } from "../react-auth0-spa";
5+
import config from "../auth_config.json";
6+
7+
const { apiOrigin = "http://localhost:3001" } = config;
58

69
const ExternalApi = () => {
710
const [showResult, setShowResult] = useState(false);
@@ -12,7 +15,7 @@ const ExternalApi = () => {
1215
try {
1316
const token = await getTokenSilently();
1417

15-
const response = await fetch("/api/external", {
18+
const response = await fetch(`${apiOrigin}/api/external`, {
1619
headers: {
1720
Authorization: `Bearer ${token}`
1821
}

0 commit comments

Comments
 (0)