forked from auth0/auth0-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
94 lines (90 loc) · 2.79 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Auth0 React</title>
</head>
<body>
<div id="app"></div>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/babel-polyfill@6/dist/polyfill.min.js"></script>
<script
crossorigin
src="https://unpkg.com/react@16/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
></script>
<script src="auth0-react.js"></script>
<script type="text/babel" data-presets="es2015,es2016,es2017,stage-3,react">
const { Auth0Provider, useAuth0 } = reactAuth0;
const { useState } = React;
const App = () => {
const {
isAuthenticated,
isLoading,
getIdTokenClaims,
getAccessTokenSilently,
getAccessTokenWithPopup,
loginWithRedirect,
loginWithPopup,
logout,
} = useAuth0();
const [token, setToken] = useState(null);
const [claims, setClaims] = useState(null);
if (isLoading) {
return <div>loading...</div>;
}
return isAuthenticated ? (
<div>
<button
onClick={() => logout({ returnTo: window.location.origin })}
>
logout
</button>
<button
onClick={async () => setToken(await getAccessTokenSilently())}
>
Get access token
</button>
<button
onClick={async () => setToken(await getAccessTokenWithPopup())}
>
Get token with popup
</button>
<button onClick={async () => setClaims(await getIdTokenClaims())}>
Get id_token claims
</button>
{token && <pre>access_token: {token}</pre>}
{claims && (
<pre>id_token_claims: {JSON.stringify(claims, null, 2)}</pre>
)}
</div>
) : (
<div>
<button onClick={() => loginWithRedirect()}>
Login with redirect
</button>
<button onClick={() => loginWithPopup()}>Login with popup</button>
</div>
);
};
ReactDOM.render(
<Auth0Provider
domain="brucke.auth0.com"
clientId="wLSIP47wM39wKdDmOj6Zb5eSEw3JVhVp"
redirectUri={window.location.origin}
>
<App />
</Auth0Provider>,
document.getElementById('app')
);
</script>
</body>
</html>