Skip to content

Commit

Permalink
Merge pull request #9 from garronej/demo
Browse files Browse the repository at this point in the history
Demo app: refactor tanstack as react-react
  • Loading branch information
garronej authored Jan 3, 2024
2 parents 3d12265 + cdc52b0 commit ca29f20
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 238 deletions.
16 changes: 8 additions & 8 deletions examples/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
"oidc-spa": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.20.1",
"vite-tsconfig-paths": "^4.2.2",
"react-router-dom": "^6.21.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.55.0",
"eslint": "^8.56.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.2.2",
"typescript": "^5.3.3",
"vite-tsconfig-paths": "^4.2.3",
"vite": "^5.0.10"
}
}
2 changes: 1 addition & 1 deletion examples/react-router/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from "react-dom/client";
import "./index.css";
import { OidcProvider } from "oidc";
import { RouterProvider } from "react-router-dom";
import { router } from "./router/router.tsx";
import { router } from "./router/router";

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
Expand Down
192 changes: 108 additions & 84 deletions examples/react-router/yarn.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions examples/tanstack-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-router": "^0.0.1-beta.283",
"oidc-spa": "^2.3.1",
"@tanstack/react-router": "^1.1.0",
"oidc-spa": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"vite-tsconfig-paths": "^4.2.2"
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.55.0",
"eslint": "^8.56.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.2.2",
"vite": "^5.0.8",
"ts-node": "^10.9.2"
"typescript": "^5.3.3",
"vite": "^5.0.10",
"ts-node": "^10.9.2",
"vite-tsconfig-paths": "^4.2.3"
}
}
2 changes: 1 addition & 1 deletion examples/tanstack-router/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { OidcProvider } from "oidc";
import { router } from "./router/router.tsx";
import { RouterProvider } from "@tanstack/react-router";
import { router } from "router/router";

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
Expand Down
44 changes: 15 additions & 29 deletions examples/tanstack-router/src/oidc.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import { useMemo } from "react";
import { createOidcProvider, useOidc } from "oidc-spa/react";
import { decodeJwt } from "oidc-spa";
import { createOidcProvider, createUseOidc } from "oidc-spa/react";
import { z } from "zod";

export const { OidcProvider, prOidc } = createOidcProvider({
clientId: import.meta.env.VITE_OIDC_CLIENT_ID,
issuerUri: import.meta.env.VITE_OIDC_ISSUER
issuerUri: import.meta.env.VITE_OIDC_ISSUER,
publicUrl: import.meta.env.BASE_URL
});

// Convenience hook to get the parsed idToken
// To call only when the user is logged in
export function useUser() {
const { oidc } = useOidc();

if (!oidc.isUserLoggedIn) {
throw new Error("This hook should be used only on authenticated routes");
}

// NOTE: When idToken changes, the component get re-rendered
// so idToken can be used in dependency arrays. ✅
const { idToken } = oidc.getTokens();

const user = useMemo(
() =>
decodeJwt(idToken) as {
// Use https://jwt.io/ to tell what's in your idToken
sub: string;
preferred_username: string;
},
[idToken]
);

return { user };
}
export const { useOidc } = createUseOidc({
// This parameter is optional, it allows you to validate the shape of the idToken
// so that you can trust that oidcTokens.decodedIdToken is of the expected shape when the user is logged in.
// In most application you do not need to look into the JWT of the idToken on the frontend
// you usually obtain the user info by querying a GET /user endpoint with a authorization header
// like `Bearer <accessToken>`.
decodedIdTokenZodSchema: z.object({
sub: z.string(),
preferred_username: z.string()
})
});
13 changes: 12 additions & 1 deletion examples/tanstack-router/src/pages/ProtectedPage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { useOidc } from "oidc";

export function ProtectedPage() {
return <h1>Protected Page</h1>;
// Here we can safely assume that the user is logged in.
const { oidcTokens } = useOidc({ assertUserLoggedIn: true });

return (
<h4>
Hello {oidcTokens.decodedIdToken.preferred_username}
<br />
The page you are currently viewing can only be accessed when you are authenticated.
</h4>
);
}
14 changes: 11 additions & 3 deletions examples/tanstack-router/src/pages/PublicPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Link } from "@tanstack/react-router";
import reactLogo from "assets/react.svg";
import viteLogo from "assets/vite.svg";

export function PublicPage() {
return (
<>
<h1>Public Page</h1>
<Link to="/protected">Link to protected route</Link>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h4>This is a page that do not requires the user to be authenticated</h4>
</>
);
}
49 changes: 49 additions & 0 deletions examples/tanstack-router/src/router/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Link } from "@tanstack/react-router";
import { useOidc } from "oidc";

export function Header() {
const { isUserLoggedIn, login, logout, oidcTokens } = useOidc();
return (
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
position: "absolute",
top: 0,
left: 0,
width: "100%"
}}
>
<span>OIDC-SPA + Tanstack-router Starter</span>

<div>
<Link to="/">
{({ isActive }) => (
<span style={{ fontWeight: isActive ? "bold" : "normal" }}>Home</span>
)}
</Link>
&nbsp; &nbsp; &nbsp;
<Link to="/protected">
{({ isActive }) => (
<span style={{ fontWeight: isActive ? "bold" : "normal" }}>
My protected page
</span>
)}
</Link>
</div>

{isUserLoggedIn ? (
<div>
<span>Hello {oidcTokens.decodedIdToken.preferred_username}</span>
&nbsp; &nbsp;
<button onClick={() => logout({ redirectTo: "home" })}>Logout</button>
</div>
) : (
<div>
<button onClick={() => login({ doesCurrentHrefRequiresAuth: false })}>Login</button>
</div>
)}
</div>
);
}
14 changes: 2 additions & 12 deletions examples/tanstack-router/src/router/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import reactLogo from "assets/react.svg";
import viteLogo from "assets/vite.svg";
import "./App.css";
import { Outlet } from "@tanstack/react-router";
import { AuthStatus } from "../AuthStatus";
import { Header } from "./Header";

export function Layout() {
return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<Header />
<Outlet />
<AuthStatus />
</>
);
}
5 changes: 4 additions & 1 deletion examples/tanstack-router/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths()]
plugins: [react(), tsconfigPaths()],
resolve: {
preserveSymlinks: true
}
});
Loading

0 comments on commit ca29f20

Please sign in to comment.