-
Notifications
You must be signed in to change notification settings - Fork 0
Add token refresh sample and improve error handling #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
334a750
Add token refresh sample and improve error handling
ksroda-sa 32a9aa8
prettier
ksroda-sa 0f6584b
Bump actions to v6 in test-js.yml and run prettier on token-refresh
ksroda-sa 22ffc00
Address Copilot review: add loading state, error test, refresh test
ksroda-sa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,5 @@ | ||
| import React from "react"; | ||
| import ReactDOM from "react-dom/client"; | ||
|
|
||
| import App from "./App"; | ||
|
|
||
| ReactDOM.createRoot(document.getElementById("root")!).render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode>, | ||
| ); | ||
| ReactDOM.createRoot(document.getElementById("root")!).render(<App />); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ISSUER_URL=https://your-tenant.us.secureauth.com/your-workspace | ||
| CLIENT_ID=your-client-id | ||
| REDIRECT_URI=http://localhost:3000/callback | ||
| SCOPES=openid profile email offline_access |
940 changes: 940 additions & 0 deletions
940
samples/react/token-refresh/.yarn/releases/yarn-4.13.0.cjs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| defaultSemverRangePrefix: "" | ||
|
|
||
| nodeLinker: node-modules | ||
|
|
||
| npmRegistryServer: "https://registry.npmjs.org" | ||
|
|
||
| yarnPath: .yarn/releases/yarn-4.13.0.cjs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # React SPA — Token Refresh | ||
|
|
||
| Minimal React app demonstrating token refresh using `react-oidc-context` with the `offline_access` scope. Tokens are refreshed automatically before they expire, and can also be refreshed manually via a button. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - `offline_access` scope enabled on the client | ||
| - `refresh_token` grant type enabled in workspace OAuth settings and in the application's Grant Types | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Copy `.env.example` to `.env` and fill in your SecureAuth values | ||
| 2. `yarn install` | ||
| 3. `yarn dev` | ||
| 4. Open http://localhost:3000 | ||
|
|
||
| ## What this demonstrates | ||
|
|
||
| - OIDC configuration with `offline_access` scope for refresh tokens | ||
| - Automatic token refresh via refresh tokens (no iframe needed) | ||
| - Manual token refresh via `signinSilent()` button | ||
| - Displaying token expiry information | ||
| - Error handling with OAuth error hints |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>SecureAuth React Token Refresh</title> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/index.tsx"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "@ciam-quickstart/react-token-refresh", | ||
| "private": true, | ||
| "packageManager": "yarn@4.13.0", | ||
| "version": "0.0.1", | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "tsc && vite build", | ||
| "preview": "vite preview", | ||
| "test": "vitest run", | ||
| "format": "prettier --write .", | ||
| "format:check": "prettier --check ." | ||
| }, | ||
| "dependencies": { | ||
| "oidc-client-ts": "3.5.0", | ||
| "react": "19.2.5", | ||
| "react-dom": "19.2.5", | ||
| "react-oidc-context": "3.3.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@testing-library/dom": "10.4.1", | ||
| "@testing-library/jest-dom": "6.9.1", | ||
| "@testing-library/react": "16.3.2", | ||
| "@types/react": "19.2.14", | ||
| "@types/react-dom": "19.2.3", | ||
| "@vitejs/plugin-react": "6.0.1", | ||
| "happy-dom": "20.8.9", | ||
| "prettier": "3.8.2", | ||
| "typescript": "6.0.2", | ||
| "vite": "8.0.8", | ||
| "vitest": "4.1.4" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { cleanup, fireEvent, render, screen } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mockUseAuth = vi.fn(); | ||
| const mockSigninSilent = vi.fn(); | ||
|
|
||
| vi.mock("react-oidc-context", () => ({ | ||
| AuthProvider: ({ children }: { children: React.ReactNode }) => ( | ||
| <>{children}</> | ||
| ), | ||
| useAuth: () => mockUseAuth(), | ||
| })); | ||
|
|
||
| import App from "./App"; | ||
|
|
||
| describe("App (Token Refresh)", () => { | ||
| afterEach(() => cleanup()); | ||
| it("renders sign in button when not authenticated", () => { | ||
| mockUseAuth.mockReturnValue({ | ||
| isAuthenticated: false, | ||
| isLoading: false, | ||
| }); | ||
| render(<App />); | ||
| expect(screen.getByText("Sign in")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows token info and refresh button when authenticated", () => { | ||
| mockUseAuth.mockReturnValue({ | ||
| isAuthenticated: true, | ||
| isLoading: false, | ||
| signinSilent: mockSigninSilent, | ||
| user: { | ||
| profile: { given_name: "Jane" }, | ||
| expires_at: Math.floor(Date.now() / 1000) + 3600, | ||
| }, | ||
| }); | ||
| render(<App />); | ||
| expect(screen.getByText("Welcome, Jane")).toBeInTheDocument(); | ||
| expect(screen.getByText("Refresh token now")).toBeInTheDocument(); | ||
| }); | ||
|
ksroda-sa marked this conversation as resolved.
|
||
|
|
||
| it("renders error message when authentication fails", () => { | ||
| mockUseAuth.mockReturnValue({ | ||
| isAuthenticated: false, | ||
| isLoading: false, | ||
| error: new Error("Unable to refresh token"), | ||
| }); | ||
| render(<App />); | ||
| expect(screen.getByText(/Unable to refresh token/)).toBeInTheDocument(); | ||
| expect(screen.getByText("Try again")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("calls signinSilent when refresh button is clicked", () => { | ||
| mockUseAuth.mockReturnValue({ | ||
| isAuthenticated: true, | ||
| isLoading: false, | ||
| signinSilent: mockSigninSilent, | ||
| user: { | ||
| profile: { given_name: "Jane" }, | ||
| expires_at: Math.floor(Date.now() / 1000) + 3600, | ||
| }, | ||
| }); | ||
| render(<App />); | ||
| fireEvent.click(screen.getByText("Refresh token now")); | ||
| expect(mockSigninSilent).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // @snippet:step1:start | ||
| // @description Import the OIDC library | ||
| import { AuthProvider, useAuth } from "react-oidc-context"; | ||
| // @snippet:step1:end | ||
|
|
||
| // @snippet:step2:start | ||
| // @description Request offline_access scope to receive a refresh token | ||
| const oidcConfig = { | ||
| authority: import.meta.env.ISSUER_URL, | ||
| client_id: import.meta.env.CLIENT_ID, | ||
| redirect_uri: import.meta.env.REDIRECT_URI, | ||
| scope: import.meta.env.SCOPES, | ||
| }; | ||
| // @snippet:step2:end | ||
|
|
||
| // @snippet:step3:start | ||
| // @description Display token status and trigger manual refresh | ||
| function TokenStatus() { | ||
| const auth = useAuth(); | ||
|
|
||
| if (auth.isLoading) { | ||
| return <p>Loading...</p>; | ||
| } | ||
|
|
||
| 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 <button onClick={() => auth.signinRedirect()}>Sign in</button>; | ||
| } | ||
|
ksroda-sa marked this conversation as resolved.
|
||
|
|
||
| const expiresAt = auth.user?.expires_at | ||
| ? new Date(auth.user.expires_at * 1000).toLocaleTimeString() | ||
| : "unknown"; | ||
|
|
||
| const handleRefresh = async () => { | ||
| try { | ||
| await auth.signinSilent(); | ||
| } catch (error) { | ||
| console.error("Token refresh failed:", error); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <p>Welcome, {auth.user?.profile.given_name}</p> | ||
| <p>Token expires at: {expiresAt}</p> | ||
| <button onClick={handleRefresh}>Refresh token now</button> | ||
| </div> | ||
| ); | ||
| } | ||
| // @snippet:step3:end | ||
|
|
||
| // @snippet:step4:start | ||
| // @description Wrap your app with AuthProvider | ||
| export default function App() { | ||
| return ( | ||
| <AuthProvider {...oidcConfig}> | ||
| <h1>SecureAuth Token Refresh Demo</h1> | ||
| <TokenStatus /> | ||
| </AuthProvider> | ||
| ); | ||
| } | ||
| // @snippet:step4:end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import ReactDOM from "react-dom/client"; | ||
|
|
||
| import App from "./App"; | ||
|
|
||
| ReactDOM.createRoot(document.getElementById("root")!).render(<App />); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /// <reference types="vite/client" /> | ||
|
|
||
| interface ImportMetaEnv { | ||
| readonly ISSUER_URL: string; | ||
| readonly CLIENT_ID: string; | ||
| readonly REDIRECT_URI: string; | ||
| readonly SCOPES: string; | ||
| } | ||
|
|
||
| interface ImportMeta { | ||
| readonly env: ImportMetaEnv; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2020", | ||
| "lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
| "module": "ESNext", | ||
| "moduleResolution": "bundler", | ||
| "jsx": "react-jsx", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "types": ["@testing-library/jest-dom"] | ||
| }, | ||
| "include": ["src", "vitest.setup.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import react from "@vitejs/plugin-react"; | ||
| import { defineConfig, loadEnv } from "vite"; | ||
|
|
||
| const exposedEnvVars = ["ISSUER_URL", "CLIENT_ID", "REDIRECT_URI", "SCOPES"]; | ||
|
|
||
| export default defineConfig(({ mode, command }) => { | ||
| const env = loadEnv(mode, process.cwd(), ""); | ||
| if (command === "serve") { | ||
| const missing = exposedEnvVars.filter((key) => !env[key]); | ||
| if (missing.length > 0) { | ||
| throw new Error( | ||
| `Missing required env vars: ${missing.join(", ")}. Copy .env.example to .env and fill in the values.`, | ||
| ); | ||
| } | ||
| } | ||
| return { | ||
| plugins: [react()], | ||
| define: Object.fromEntries( | ||
| exposedEnvVars.map((key) => [ | ||
| `import.meta.env.${key}`, | ||
| JSON.stringify(env[key] ?? ""), | ||
| ]), | ||
| ), | ||
| server: { | ||
| port: 3000, | ||
| }, | ||
| }; | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import react from "@vitejs/plugin-react"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [react()], | ||
| test: { | ||
| environment: "happy-dom", | ||
| setupFiles: ["./vitest.setup.ts"], | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import "@testing-library/jest-dom/vitest"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.