@lpshanley/oidc-start-react 0.1.0-next.0 → 0.1.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,18 +1,112 @@
|
|
|
1
1
|
# @lpshanley/oidc-start-react
|
|
2
2
|
|
|
3
|
-
TanStack Start
|
|
3
|
+
Add OpenID Connect sign-in to a TanStack Start app. Loaders can read the current user, components can start sign-in or sign-out, and Start’s middleware handles the session cookies. The code exchange and provider tokens stay on the server.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This guide adds two routes to an existing app. The home page at `/` shows who is signed in. The callback at `/callback` receives the browser when the provider sends it back. [Complete example app](https://github.com/lpshanley/oidc-start/tree/main/examples/tanstack-start)
|
|
6
|
+
|
|
7
|
+
## 1. Install and configure the server
|
|
8
|
+
|
|
9
|
+
Use Node.js 22.12 or newer. Your app supplies `@tanstack/react-start@^1.169.2` and `@tanstack/react-router@^1.170.44`. Install both OIDC packages because your app imports the core directly.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @lpshanley/oidc-start-react@next @lpshanley/oidc-start-core@next
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Register a confidential web client with your OIDC provider. If your app runs at `http://localhost:5173`, register these exact URLs. Use your public HTTPS origin in production.
|
|
16
|
+
|
|
17
|
+
| Provider setting | Local value |
|
|
18
|
+
| --- | --- |
|
|
19
|
+
| Sign-in redirect URI | `http://localhost:5173/callback` |
|
|
20
|
+
| Post-logout redirect URI | `http://localhost:5173/` |
|
|
21
|
+
|
|
22
|
+
Set these values in your **server environment**. Generate `SESSION_SECRET` once with `openssl rand -hex 32` and reuse it across server instances. Your provider must supply the account’s email in the ID token or through UserInfo.
|
|
23
|
+
|
|
24
|
+
```dotenv
|
|
25
|
+
APP_ORIGIN=http://localhost:5173
|
|
26
|
+
OIDC_ISSUER=https://id.example.com
|
|
27
|
+
OIDC_CLIENT_ID=your-client-id
|
|
28
|
+
OIDC_CLIENT_SECRET=your-client-secret
|
|
29
|
+
# Paste the generated secret here before starting the app.
|
|
30
|
+
SESSION_SECRET=
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
For local development, copy the example’s `auth.server.ts` into `src/auth.server.ts`. It exports an `auth` instance configured with `callbackPath: '/callback'` and `postLogoutPath: '/'`. The example admits any authenticated user and keeps revocations in memory. For deployment, set `allowSession` to your access policy and use a durable revocation store shared by all instances. [Copy auth.server.ts](https://github.com/lpshanley/oidc-start/blob/main/examples/tanstack-start/src/auth.server.ts) · [Core configuration and storage](https://www.npmjs.com/package/@lpshanley/oidc-start-core)
|
|
34
|
+
|
|
35
|
+
## 2. Register the request middleware
|
|
36
|
+
|
|
37
|
+
In `src/start.ts`, add Start’s CSRF middleware before the OIDC middleware. The first checks the origin of browser calls to server functions, including sign-out. The second resolves the session for the request and writes any refreshed cookies. The `createServerOnlyFn` wrapper keeps your `auth` instance out of the client bundle.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { oidcMiddleware } from '@lpshanley/oidc-start-react'
|
|
41
|
+
import { createCsrfMiddleware, createServerOnlyFn, createStart } from '@tanstack/react-start'
|
|
42
|
+
import { auth } from './auth.server'
|
|
43
|
+
|
|
44
|
+
const csrf = createCsrfMiddleware({ filter: ctx => ctx.handlerType === 'serverFn' })
|
|
45
|
+
const oidc = oidcMiddleware({ core: createServerOnlyFn(() => auth) })
|
|
46
|
+
|
|
47
|
+
export const startInstance = createStart(() => ({
|
|
48
|
+
requestMiddleware: [csrf, oidc],
|
|
49
|
+
}))
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## 3. Add the callback route
|
|
53
|
+
|
|
54
|
+
Create `src/routes/callback.ts`. The handler checks the provider response, establishes the session, and redirects to the local path chosen at sign-in.
|
|
6
55
|
|
|
7
56
|
```ts
|
|
8
|
-
import { createFileRoute } from '@tanstack/react-router'
|
|
9
57
|
import { handleCallbackRoute } from '@lpshanley/oidc-start-react'
|
|
58
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
10
59
|
|
|
11
|
-
export const Route = createFileRoute('/
|
|
60
|
+
export const Route = createFileRoute('/callback')({
|
|
12
61
|
server: { handlers: { GET: handleCallbackRoute() } },
|
|
13
62
|
})
|
|
14
63
|
```
|
|
15
64
|
|
|
16
|
-
|
|
65
|
+
## 4. Connect the buttons
|
|
66
|
+
|
|
67
|
+
In `src/routes/index.tsx`, call `getAuth()` from the loader. It returns `{ user }`, with `user: null` for a signed-out visitor. Bind `signOut` with `useServerFn` so TanStack follows the redirect to the provider when the user clicks the button.
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import { displayName } from '@lpshanley/oidc-start-core/models'
|
|
71
|
+
import { getAuth, getSignInUrl, signOut } from '@lpshanley/oidc-start-react/functions'
|
|
72
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
73
|
+
import { useServerFn } from '@tanstack/react-start'
|
|
74
|
+
|
|
75
|
+
export const Route = createFileRoute('/')({
|
|
76
|
+
loader: () => getAuth(),
|
|
77
|
+
component: Home,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
function Home() {
|
|
81
|
+
const { user } = Route.useLoaderData()
|
|
82
|
+
const signOutAndRedirect = useServerFn(signOut)
|
|
83
|
+
|
|
84
|
+
if (!user) {
|
|
85
|
+
return <button onClick={async () => location.assign(await getSignInUrl())}>Sign in</button>
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<main>
|
|
90
|
+
<p>Signed in as {displayName(user)}.</p>
|
|
91
|
+
<button onClick={() => signOutAndRedirect()}>Sign out</button>
|
|
92
|
+
</main>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Try it.** Open `/` in a fresh browser session and choose **Sign in**. After the provider redirects you back, your name should appear and survive a reload. Choose **Sign out**, finish logout at the provider if prompted, and reload. You should see **Sign in** again.
|
|
98
|
+
|
|
99
|
+
To return someone to a specific page, pass a local path when starting sign-in, for example `getSignInUrl({ data: { returnPathname: '/account' } })`.
|
|
100
|
+
|
|
101
|
+
## HTTP routes and server access
|
|
102
|
+
|
|
103
|
+
The buttons above use server functions. If you prefer ordinary links and forms, mount these handlers from `@lpshanley/oidc-start-react` behind the same OIDC middleware. Choose the paths in your app.
|
|
104
|
+
|
|
105
|
+
| Method | Handler | When to use it |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| GET | `handleSignInRoute()` | Link to sign-in. The optional `returnPathname` query parameter selects a local return path. |
|
|
108
|
+
| GET | `handleCallbackRoute()` | Mount at the exact callback path registered with your provider. |
|
|
109
|
+
| POST | `handleSignOutRoute()` | Submit a same-origin form. Add `server.middleware: [createCsrfMiddleware()]` to this route because the global filter above only covers server functions. |
|
|
110
|
+
| POST | `handleBackchannelLogoutRoute()` | Register this URL with your provider for back-channel logout. The provider sends a signed logout token without a browser Origin header. Keep the browser CSRF check scoped to the browser routes. |
|
|
17
111
|
|
|
18
|
-
|
|
112
|
+
In a server handler, import `getOidcContext` from `@lpshanley/oidc-start-react/server` to access the resolved session and core for the current request. Use `getAuth()` from `/functions` when a loader or component only needs the user.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lpshanley/oidc-start-react",
|
|
3
|
-
"version": "0.1.0-next.
|
|
3
|
+
"version": "0.1.0-next.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/lpshanley/oidc-start.git",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"node": ">=22.12.0"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@lpshanley/oidc-start-core": "0.1.0-next.
|
|
38
|
+
"@lpshanley/oidc-start-core": "0.1.0-next.1",
|
|
39
39
|
"zod": "4.6.7"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|