You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 03-Calling-an-API/README.md
+35-59Lines changed: 35 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,41 +68,39 @@ For **Signing Algorithm**, select **RS256**.
68
68
69
69

70
70
71
-
### Modify the Backend API
71
+
### Create the Backend API
72
72
73
-
For this tutorial, let's modify the API to include a new endpoint that expects an Access Token to be supplied.
73
+
For this example, you'll create an [Express](https://expressjs.com/) server that acts as the backend API. This API will expose an endpoint to validate incoming ID Tokens before returning a response.
74
74
75
-
> **Note** In a real scenario, this work would be done by the external API that is to be called from the frontend. This new endpoint is simply a convenience to serve as a learning exercise.
75
+
Start by installing the following packages:
76
76
77
-
Open `server.js` and add a new Express route to serve as the API endpoint, right underneath the existing one:
msg:"Your Access Token was successfully validated!"
95
-
});
96
-
});
97
-
```
86
+
Next, create a new file `server.js` with the following code:
98
87
99
-
Notice that it continues to use the same `checkJwt` middleware in order to validate the Access Token. The difference here is that the Access Token must be validated using the API identifier, rather than the client ID that we used for the ID Token.
88
+
```js
89
+
constexpress=require("express");
90
+
constjwt=require("express-jwt");
91
+
constjwksRsa=require("jwks-rsa");
100
92
101
-
> **Note** The API identifier is the identifer that was specified when the API was created in the [Auth0 dashboard](https://manage.auth0.com/#/apis).
93
+
// Create a new Express app
94
+
constapp=express();
102
95
103
-
Therefore, modify the `checkJwt` function to include the API identifier value in the `audience` setting:
96
+
// Set up Auth0 configuration
97
+
constauthConfig= {
98
+
domain:"<YOUR AUTH0 DOMAIN>",
99
+
audience:"<YOUR AUTH0 API IDENTIFIER>"
100
+
};
104
101
105
-
```js
102
+
// Define middleware that validates incoming bearer tokens
> **Note** As the `audience` property accepts an array of values, both the client ID and the API identifier can be given, allowing both the ID Token and the Access Token to be verified using the same middleware.
122
-
123
-
Finally, modify the `authConfig` object to include your `audience` value:
124
-
125
-
```js
126
-
constauthConfig= {
127
-
domain:"<YOUR AUTH0 DOMAIN>",
128
-
clientID:"<YOUR AUTH0 CLIENT ID>",
129
-
audience:"<YOUR AUTH0 API IDENTIFIER>"
130
-
};
131
-
```
132
116
133
-
Finally, modify `package.json` to add two new scripts `dev` and `api` that can be used to start the frontend and the backend API together:
134
-
135
-
```json
136
-
{
137
-
"name": "03-calling-an-api",
138
-
"version": "0.1.0",
139
-
"private": true,
140
-
"scripts": {
141
-
"serve": "vue-cli-service serve",
142
-
"build": "vue-cli-service build",
143
-
"lint": "vue-cli-service lint",
144
-
"dev": "npm-run-all --parallel serve api",
145
-
"api": "node server.js"
146
-
}
117
+
// Define an endpoint that must be called with an access token
msg:"Your Access Token was successfully validated!"
121
+
});
122
+
});
147
123
148
-
// .. package dependencies and other JSON nodes
149
-
}
124
+
//Start the app
125
+
app.listen(3001, () =>console.log("API listening on 3001"));
150
126
```
151
127
152
-
You can now start the project using `npm run dev` in the terminal, and the frontend Vue.js application will start up alongside the backend API.
128
+
The above API has one available endpoint, `/api/external`, that returns a JSON response to the caller. This endpoint uses the `checkJwt` middleware to validate the supplied bearer token using your tenant's [JSON Web Key Set](https://auth0.com/docs/jwks). If the token is valid, the request is allowed to continue. Otherwise, the server returns a 401 Unauthorized response.
0 commit comments