forked from auth0/auth0-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
155 lines (143 loc) · 4.61 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
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
<!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@18/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18/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;
function useLocalStorage(key, initialValue) {
const [storedValue, setStoredValue] = useState(() => {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
});
const setValue = (value) => {
setStoredValue(value);
window.localStorage.setItem(key, JSON.stringify(value));
};
return [storedValue, setValue];
}
const Playground = ({ onChangeIDP }) => {
const {
user,
isAuthenticated,
isLoading,
getIdTokenClaims,
getAccessTokenSilently,
getAccessTokenWithPopup,
loginWithRedirect,
loginWithPopup,
logout,
} = useAuth0();
const [token, setToken] = useState(null);
const [claims, setClaims] = useState(null);
const changeToAuth0 = () => {
onChangeIDP({
domain: 'brucke.auth0.com',
clientId: 'wLSIP47wM39wKdDmOj6Zb5eSEw3JVhVp',
});
};
const changeToNodeOidc = () => {
onChangeIDP({
domain: 'http://127.0.0.1:3000',
clientId: 'testing',
});
};
if (isLoading) {
return <div>loading...</div>;
}
return isAuthenticated ? (
<div>
<div id="hello">Hello, {user.name}!</div>
<button
id="logout"
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 id="login" onClick={() => loginWithRedirect()}>
Login with redirect
</button>
<button onClick={() => loginWithPopup()}>Login with popup</button>
<button data-cy="use-auth0" onClick={() => changeToAuth0()}>
Use Auth0
</button>
<button
data-cy="use-node-oidc-provider"
onClick={() => changeToNodeOidc()}
>
Use Node OIDC Provider
</button>
</div>
);
};
const App = () => {
const [identityProvider, setIdentityProvider] = useLocalStorage(
'identity_provider',
{
domain: 'brucke.auth0.com',
clientId: 'wLSIP47wM39wKdDmOj6Zb5eSEw3JVhVp',
}
);
const changeIDP = (data) => {
setIdentityProvider(data);
};
return (
<React.StrictMode>
<Auth0Provider
domain={identityProvider.domain}
clientId={identityProvider.clientId}
key={identityProvider.domain}
authorizationParams={{
redirect_uri: window.location.origin,
}}
>
<Playground onChangeIDP={changeIDP} />
</Auth0Provider>
</React.StrictMode>
);
};
const root = ReactDOM.createRoot(document.getElementById('app'));
root.render(<App />);
</script>
</body>
</html>