-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
104 lines (88 loc) · 3.04 KB
/
index.html
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
---
title: GraphiQL
---
<html>
<head>
<title>{{ page.title }}</title>
<link rel="icon" href="/images/favicon.ico">
<meta name=color-scheme content="light dark">
<link href="https://unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" />
</head>
<body style="margin: 0;">
<div id="graphiql" style="height: 100vh;"></div>
<script crossorigin src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script crossorigin src="https://unpkg.com/graphiql/graphiql.min.js"></script>
<script crossorigin src="https://cdn.auth0.com/js/auth0-spa-js/1.22/auth0-spa-js.production.js"></script>
<script>
let auth0 = null;
window.onload = async () => {
await configureClient();
const isAuthenticated = await auth0.isAuthenticated();
if (isAuthenticated) {
console.log("> User is authenticated");
window.history.replaceState({}, document.title, window.location.pathname);
renderUI();
return;
}
console.log("> User not authenticated");
const query = window.location.search;
const shouldParseResult = query.includes("code=") && query.includes("state=");
if (shouldParseResult) {
console.log("> Parsing redirect");
try {
await auth0.handleRedirectCallback();
console.log("Logged in!");
} catch (err) {
console.log("Error parsing redirect:", err);
}
window.history.replaceState({}, document.title, "/");
renderUI();
} else {
await login();
}
};
const options = { audience: "https://api.compada.dev/graphql" }
const login = async () => {
try {
console.log("Logging in");
await auth0.loginWithRedirect({redirect_uri: window.location.origin, ...options});
} catch (err) {
console.log("Log in failed", err);
}
};
const logout = () => {
try {
console.log("Logging out");
auth0.logout({
returnTo: window.location.origin
});
} catch (err) {
console.log("Log out failed", err);
}
};
const configureClient = async () => {
auth0 = await createAuth0Client({
domain: '{{ site.auth0_domain }}',
client_id: '{{ site.auth0_client_id }}'
});
auth0.checkSession();
};
const renderUI = async () => {
console.log("Rendering UI")
const accessToken = await auth0.getTokenSilently(options);
console.log("> User is authorized")
const fetcher = GraphiQL.createFetcher({
url: '{{ site.api_url }}',
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
ReactDOM.render(
React.createElement(GraphiQL, { fetcher: fetcher }),
document.getElementById('graphiql'),
);
};
</script>
</body>
</html>