keycloak-theme-cloud/src/App/App.tsx

86 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-09-06 19:22:23 +02:00
import "./App.css";
import logo from "./logo.svg";
import myimg from "./myimg.png";
2024-01-10 13:15:22 +01:00
import { OidcProvider, useOidc, getKeycloakAccountUrl } from "./oidc";
2022-09-06 19:22:23 +02:00
export default function App() {
return (
2024-01-10 13:38:55 +01:00
// To integrate Keycloak to your React App you have many options such as:
// - https://www.npmjs.com/package/keycloak-js
// - https://github.com/authts/oidc-client-ts
// - https://github.com/authts/react-oidc-context
// In this starter we use oidc-spa instead
// It's a new library made by us, the Keycloakify team.
// Check it out: https://github.com/keycloakify/oidc-spa
2023-10-22 12:39:00 +02:00
<OidcProvider>
<ContextualizedApp />
2023-10-22 12:39:00 +02:00
</OidcProvider>
);
}
function ContextualizedApp() {
2024-01-10 13:15:22 +01:00
const { isUserLoggedIn, login, logout, oidcTokens } = useOidc();
2023-10-22 12:39:00 +02:00
return (
<div className="App">
<header className="App-header">
2024-01-10 13:15:22 +01:00
{isUserLoggedIn ?
(
<>
<h1>Hello {oidcTokens.decodedIdToken.name} !</h1>
<a
href={getKeycloakAccountUrl({ locale: "en" })}
>
Link to your Keycloak account
</a>
<button
onClick={() => logout({ redirectTo: "home" })}
>
Logout
</button>
2024-01-10 13:38:55 +01:00
<Jwt />
2024-01-10 13:15:22 +01:00
</>
)
:
(
<button
onClick={() => login({
2023-10-24 13:42:23 +02:00
doesCurrentHrefRequiresAuth: false,
//extraQueryParams: { kc_idp_hint: "google" }
2024-01-10 13:15:22 +01:00
})}
>
Login
2023-10-24 13:42:23 +02:00
</button>
2024-01-10 13:15:22 +01:00
)
2023-10-22 12:39:00 +02:00
}
<img src={logo} className="App-logo" alt="logo" />
<img src={myimg} alt="test_image" />
<p style={{ "fontFamily": '"Work Sans"' }}>Hello world</p>
2023-11-23 03:52:40 +01:00
<p>Check out all keycloak pages in the <a href="https://storybook.keycloakify.dev">Storybook</a>!</p>
2023-10-22 12:39:00 +02:00
<p>Once you've identified the ones you want to customize run <code>npx eject-keycloak-page</code></p>
</header>
</div>
);
}
2024-01-10 13:38:55 +01:00
function Jwt(){
const { oidcTokens } = useOidc({
assertUserLoggedIn: true
});
// NOTE: Use `Bearer ${oidcTokens.accessToken}` as the Authorization header to call your backend
// Here we just display the decoded id token
return (
<pre style={{ textAlign: "left" }}>
{JSON.stringify(oidcTokens.decodedIdToken, null, 2)}
</pre>
);
}