-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathExternalApi.js
197 lines (176 loc) · 5.31 KB
/
ExternalApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, { useState } from "react";
import { Button, Alert } from "reactstrap";
import Highlight from "../components/Highlight";
import { useAuth0, withAuthenticationRequired } from "@auth0/auth0-react";
import { getConfig } from "../config";
import Loading from "../components/Loading";
export const ExternalApiComponent = () => {
const { apiOrigin = "http://localhost:3001", audience } = getConfig();
const [state, setState] = useState({
showResult: false,
apiMessage: "",
error: null,
});
const { getAccessTokenSilently, loginWithPopup, getAccessTokenWithPopup } =
useAuth0();
const handleConsent = async () => {
try {
await getAccessTokenWithPopup();
setState({
...state,
error: null,
});
} catch (error) {
setState({
...state,
error: error.error,
});
}
await callApi();
};
const handleLoginAgain = async () => {
try {
await loginWithPopup();
setState({
...state,
error: null,
});
} catch (error) {
setState({
...state,
error: error.error,
});
}
await callApi();
};
const callApi = async () => {
try {
const token = await getAccessTokenSilently();
const response = await fetch(`${apiOrigin}/api/external`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const responseData = await response.json();
setState({
...state,
showResult: true,
apiMessage: responseData,
});
} catch (error) {
setState({
...state,
error: error.error,
});
}
};
const handle = (e, fn) => {
e.preventDefault();
fn();
};
return (
<>
<div className="mb-5">
{state.error === "consent_required" && (
<Alert color="warning">
You need to{" "}
<a
href="#/"
class="alert-link"
onClick={(e) => handle(e, handleConsent)}
>
consent to get access to users api
</a>
</Alert>
)}
{state.error === "login_required" && (
<Alert color="warning">
You need to{" "}
<a
href="#/"
class="alert-link"
onClick={(e) => handle(e, handleLoginAgain)}
>
log in again
</a>
</Alert>
)}
<h1>External API</h1>
<p className="lead">
Ping an external API by clicking the button below.
</p>
<p>
This will call a local API on port 3001 that would have been started
if you run <code>npm run dev</code>. An access token is sent as part
of the request's `Authorization` header and the API will validate it
using the API's audience value.
</p>
{!audience && (
<Alert color="warning">
<p>
You can't call the API at the moment because your application does
not have any configuration for <code>audience</code>, or it is
using the default value of{" "}
<code>{yourApiIdentifier}</code>. You might get this
default value if you used the "Download Sample" feature of{" "}
<a href="https://auth0.com/docs/quickstart/spa/react">
the quickstart guide
</a>
, but have not set an API up in your Auth0 Tenant. You can find
out more information on{" "}
<a href="https://auth0.com/docs/api">setting up APIs</a> in the
Auth0 Docs.
</p>
<p>
The audience is the identifier of the API that you want to call
(see{" "}
<a href="https://auth0.com/docs/get-started/dashboard/tenant-settings#api-authorization-settings">
API Authorization Settings
</a>{" "}
for more info).
</p>
<p>
In this sample, you can configure the audience in a couple of
ways:
</p>
<ul>
<li>
in the <code>src/index.js</code> file
</li>
<li>
by specifying it in the <code>auth_config.json</code> file (see
the <code>auth_config.json.example</code> file for an example of
where it should go)
</li>
</ul>
<p>
Once you have configured the value for <code>audience</code>,
please restart the app and try to use the "Ping API" button below.
</p>
</Alert>
)}
<Button
color="primary"
className="mt-5"
onClick={callApi}
disabled={!audience}
>
Ping API
</Button>
</div>
<div className="result-block-container">
{state.showResult && (
<div className="result-block" data-testid="api-result">
<h6 className="muted">Result</h6>
<Highlight>
<span>{JSON.stringify(state.apiMessage, null, 2)}</span>
</Highlight>
</div>
)}
</div>
</>
);
};
export default withAuthenticationRequired(ExternalApiComponent, {
onRedirecting: () => <Loading />,
});