-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
63 lines (56 loc) · 1.72 KB
/
Copy pathApp.tsx
File metadata and controls
63 lines (56 loc) · 1.72 KB
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
// @snippet:step1:start
// @description Import the OIDC authentication library
import { AuthProvider, useAuth } from "react-oidc-context";
// @snippet:step1:end
// @snippet:step2:start
// @description Configure the OIDC client with your SecureAuth app settings
const oidcConfig = {
authority: import.meta.env.ISSUER_URL,
client_id: import.meta.env.CLIENT_ID,
redirect_uri: import.meta.env.REDIRECT_URI,
post_logout_redirect_uri: import.meta.env.POST_LOGOUT_URI,
scope: import.meta.env.SCOPES,
};
// @snippet:step2:end
// @snippet:step3:start
// @description Add login and logout buttons that redirect to SecureAuth
function AuthButtons() {
const auth = useAuth();
if (auth.isLoading) {
return <div>Loading...</div>;
}
if (auth.error) {
const hint = new URLSearchParams(window.location.search).get("error_hint");
return (
<div style={{ color: "red" }}>
<p>Error: {auth.error.message}</p>
{hint && <p>{hint}</p>}
<button onClick={() => auth.signinRedirect()}>Try again</button>
</div>
);
}
if (auth.isAuthenticated) {
return (
<div>
<p>
Welcome, {auth.user?.profile.given_name}{" "}
{auth.user?.profile.family_name} ({auth.user?.profile.email})
</p>
<button onClick={() => auth.signoutRedirect()}>Sign out</button>
</div>
);
}
return <button onClick={() => auth.signinRedirect()}>Sign in</button>;
}
// @snippet:step3:end
// @snippet:step4:start
// @description Wrap your app with the AuthProvider to enable authentication
export default function App() {
return (
<AuthProvider {...oidcConfig}>
<h1>SecureAuth React PKCE Demo</h1>
<AuthButtons />
</AuthProvider>
);
}
// @snippet:step4:end