You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This is an early-stage port of [authkit-remix](https://github.com/workos/authkit-remix) to support React Router. The features focus on framework mode (e.g. Remix), with more planned support for library mode and more features.
5
5
6
6
The AuthKit library for React Router 7+ provides convenient helpers for authentication and session management using WorkOS & AuthKit with React Router. You can find this library in action in the [react-router-authkit-example](https://github.com/workos/react-router-authkit-example) repo.
@@ -25,13 +25,13 @@ AuthKit for React Router offers a flexible configuration system that allows you
25
25
26
26
### 1. Environment Variables
27
27
28
-
The simplest way is to set environment variables in your `.env.local` file:
28
+
The simplest way is to set environment variables in your `.env.local` file:
29
29
30
-
```bash
31
-
WORKOS_CLIENT_ID="client_..."# retrieved from the WorkOS dashboard
32
-
WORKOS_API_KEY="sk_test_..."# retrieved from the WorkOS dashboard
33
-
WORKOS_REDIRECT_URI="http://localhost:5173/callback"# configured in the WorkOS dashboard
34
-
WORKOS_COOKIE_PASSWORD="<your password>"# generate a secure password here
30
+
```bash
31
+
WORKOS_CLIENT_ID="client_..."# retrieved from the WorkOS dashboard
32
+
WORKOS_API_KEY="sk_test_..."# retrieved from the WorkOS dashboard
33
+
WORKOS_REDIRECT_URI="http://localhost:5173/callback"# configured in the WorkOS dashboard
34
+
WORKOS_COOKIE_PASSWORD="<your password>"# generate a secure password here
35
35
```
36
36
37
37
### 2. Programmatic Configuration
@@ -59,17 +59,14 @@ For non-standard environments (like Deno or Edge functions), you can provide a c
59
59
60
60
> [!Warning]
61
61
>
62
-
>While this library includes support for custom environment sources that could theoretically work in non-Node.js runtimes like Deno or Edge functions, this functionality has not been extensively tested (yet). If you're planning to use AuthKit in these environments, you may encounter unexpected issues. We welcome feedback and contributions from users who test in these environments.
62
+
>While this library includes support for custom environment sources that could theoretically work in non-Node.js runtimes like Deno or Edge functions, this functionality has not been extensively tested (yet). If you're planning to use AuthKit in these environments, you may encounter unexpected issues. We welcome feedback and contributions from users who test in these environments.
|`cookieName`|`WORKOS_COOKIE_NAME`|`wos-session`| No |Name of the session cookie |
93
+
|`apiHttps`|`WORKOS_API_HTTPS`|`true`| No |Whether to use HTTPS for API calls |
94
+
|`cookieMaxAge`|`WORKOS_COOKIE_MAX_AGE`|`34560000` (400 days) | No |Maximum age of cookie in seconds |
95
+
|`apiHostname`|`WORKOS_API_HOSTNAME`|`api.workos.com`| No |WorkOS API hostname |
96
+
|`apiPort`|`WORKOS_API_PORT`| - | No |Port to use for API calls |
97
+
98
+
>[!NOTE]
102
99
>
103
-
>The `cookiePassword` must be at least 32 characters long for security reasons.
100
+
>The `cookiePassword` must be at least 32 characters long for security reasons.
104
101
105
102
## Setup
106
103
@@ -161,14 +158,7 @@ export function App() {
161
158
For pages where you want to display a signed-in and signed-out view, use `authkitLoader` to retrieve the user profile from WorkOS. You can pass in additional data by providing a loader function directly to `authkitLoader`.
**Note:** Only expose access tokens to the client when necessary for your use case (e.g., making direct API calls from the browser). Consider alternatives like:
279
+
284
280
- Making API calls server-side in your loaders
285
281
- Creating proxy endpoints in your application
286
282
- Using separate client-specific tokens with limited scope
@@ -291,23 +287,27 @@ When using the `ensureSignedIn` option, you can be confident that `getAccessToke
// With ensureSignedIn: true, the user is guaranteed to be authenticated
296
-
const accessToken =getAccessToken();
297
-
298
-
// Use the token for your API calls
299
-
const data =awaitfetchProtectedData(accessToken);
300
-
301
-
return { data };
302
-
}, { ensureSignedIn: true });
290
+
authkitLoader(
291
+
args,
292
+
async ({ auth, getAccessToken }) => {
293
+
// With ensureSignedIn: true, the user is guaranteed to be authenticated
294
+
const accessToken =getAccessToken();
295
+
296
+
// Use the token for your API calls
297
+
const data =awaitfetchProtectedData(accessToken);
298
+
299
+
return { data };
300
+
},
301
+
{ ensureSignedIn: true },
302
+
);
303
303
```
304
304
305
305
### Using withAuth for low-level access
306
306
307
307
For advanced use cases, the `withAuth` function provides direct access to authentication data, including the access token. Unlike `authkitLoader`, this function:
308
308
309
309
- Does not handle automatic token refresh
310
-
- Does not manage cookies or session updates
310
+
- Does not manage cookies or session updates
311
311
- Returns the access token directly as a property
312
312
- Requires manual redirect handling for unauthenticated users
313
313
@@ -317,20 +317,20 @@ import { redirect, type LoaderFunctionArgs } from 'react-router';
While the standard authentication flow handles session management automatically, some use cases require manually creating and storing a session. This is useful for custom authentication flows like email verification or token exchange.
368
+
369
+
For these scenarios, you can use the `saveSession` function:
>When deploying to serverless environments like AWS Lambda, ensure you pass the same storage configuration to both your main routes and the callback route to handle cold starts properly.
497
+
>When deploying to serverless environments like AWS Lambda, ensure you pass the same storage configuration to both your main routes and the callback route to handle cold starts properly.
443
498
444
499
AuthKit works with any session storage that implements React Router's `SessionStorage` interface, including Redis-based or database-backed implementations.
0 commit comments