@lpshanley/oidc-start-react 0.1.0-next.0 → 0.1.0
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/LICENSE +21 -0
- package/README.md +102 -6
- package/package.json +3 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lucas Shanley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,18 +1,114 @@
|
|
|
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
|
+
For multi-replica deployment, see the [production guide](https://github.com/lpshanley/oidc-start/blob/main/docs/production.md) and [tested PostgreSQL example](https://github.com/lpshanley/oidc-start/tree/main/examples/postgres).
|
|
8
|
+
|
|
9
|
+
## 1. Install and configure the server
|
|
10
|
+
|
|
11
|
+
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.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @lpshanley/oidc-start-react @lpshanley/oidc-start-core
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
| Provider setting | Local value |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| Sign-in redirect URI | `http://localhost:5173/callback` |
|
|
22
|
+
| Post-logout redirect URI | `http://localhost:5173/` |
|
|
23
|
+
|
|
24
|
+
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.
|
|
25
|
+
|
|
26
|
+
```dotenv
|
|
27
|
+
APP_ORIGIN=http://localhost:5173
|
|
28
|
+
OIDC_ISSUER=https://id.example.com
|
|
29
|
+
OIDC_CLIENT_ID=your-client-id
|
|
30
|
+
OIDC_CLIENT_SECRET=your-client-secret
|
|
31
|
+
# Paste the generated secret here before starting the app.
|
|
32
|
+
SESSION_SECRET=
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
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)
|
|
36
|
+
|
|
37
|
+
## 2. Register the request middleware
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { oidcMiddleware } from '@lpshanley/oidc-start-react'
|
|
43
|
+
import { createCsrfMiddleware, createServerOnlyFn, createStart } from '@tanstack/react-start'
|
|
44
|
+
import { auth } from './auth.server'
|
|
45
|
+
|
|
46
|
+
const csrf = createCsrfMiddleware({ filter: ctx => ctx.handlerType === 'serverFn' })
|
|
47
|
+
const oidc = oidcMiddleware({ core: createServerOnlyFn(() => auth) })
|
|
48
|
+
|
|
49
|
+
export const startInstance = createStart(() => ({
|
|
50
|
+
requestMiddleware: [csrf, oidc],
|
|
51
|
+
}))
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 3. Add the callback route
|
|
55
|
+
|
|
56
|
+
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
57
|
|
|
7
58
|
```ts
|
|
8
|
-
import { createFileRoute } from '@tanstack/react-router'
|
|
9
59
|
import { handleCallbackRoute } from '@lpshanley/oidc-start-react'
|
|
60
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
10
61
|
|
|
11
|
-
export const Route = createFileRoute('/
|
|
62
|
+
export const Route = createFileRoute('/callback')({
|
|
12
63
|
server: { handlers: { GET: handleCallbackRoute() } },
|
|
13
64
|
})
|
|
14
65
|
```
|
|
15
66
|
|
|
16
|
-
|
|
67
|
+
## 4. Connect the buttons
|
|
68
|
+
|
|
69
|
+
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.
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { displayName } from '@lpshanley/oidc-start-core/models'
|
|
73
|
+
import { getAuth, getSignInUrl, signOut } from '@lpshanley/oidc-start-react/functions'
|
|
74
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
75
|
+
import { useServerFn } from '@tanstack/react-start'
|
|
76
|
+
|
|
77
|
+
export const Route = createFileRoute('/')({
|
|
78
|
+
loader: () => getAuth(),
|
|
79
|
+
component: Home,
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
function Home() {
|
|
83
|
+
const { user } = Route.useLoaderData()
|
|
84
|
+
const signOutAndRedirect = useServerFn(signOut)
|
|
85
|
+
|
|
86
|
+
if (!user) {
|
|
87
|
+
return <button onClick={async () => location.assign(await getSignInUrl())}>Sign in</button>
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<main>
|
|
92
|
+
<p>Signed in as {displayName(user)}.</p>
|
|
93
|
+
<button onClick={() => signOutAndRedirect()}>Sign out</button>
|
|
94
|
+
</main>
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**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.
|
|
100
|
+
|
|
101
|
+
To return someone to a specific page, pass a local path when starting sign-in, for example `getSignInUrl({ data: { returnPathname: '/account' } })`.
|
|
102
|
+
|
|
103
|
+
## HTTP routes and server access
|
|
104
|
+
|
|
105
|
+
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.
|
|
106
|
+
|
|
107
|
+
| Method | Handler | When to use it |
|
|
108
|
+
| --- | --- | --- |
|
|
109
|
+
| GET | `handleSignInRoute()` | Link to sign-in. The optional `returnPathname` query parameter selects a local return path. |
|
|
110
|
+
| GET | `handleCallbackRoute()` | Mount at the exact callback path registered with your provider. |
|
|
111
|
+
| POST | `handleSignOutRoute()` | Submit a same-origin form. Add `server.middleware: [createCsrfMiddleware()]` to this route because the global filter above only covers server functions. |
|
|
112
|
+
| 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
113
|
|
|
18
|
-
|
|
114
|
+
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,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lpshanley/oidc-start-react",
|
|
3
|
-
"version": "0.1.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
4
5
|
"repository": {
|
|
5
6
|
"type": "git",
|
|
6
7
|
"url": "git+https://github.com/lpshanley/oidc-start.git",
|
|
@@ -35,7 +36,7 @@
|
|
|
35
36
|
"node": ">=22.12.0"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"@lpshanley/oidc-start-core": "0.1.0
|
|
39
|
+
"@lpshanley/oidc-start-core": "0.1.0",
|
|
39
40
|
"zod": "4.6.7"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|