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
The AuthKit library for Next.js provides convenient helpers for authentication and session management using WorkOS & AuthKit with Next.js.
3
+
The AuthKit library for Remix provides convenient helpers for authentication and session management using WorkOS & AuthKit with Remix. You can find this library in action in the [remix-authkit-example](https://github.com/workos/remix-authkit-example) repo.
4
4
5
5
## Installation
6
6
7
7
Install the package with:
8
8
9
9
```
10
-
npm i @workos-inc/authkit-nextjs
10
+
npm i @workos-inc/authkit-remix
11
11
```
12
12
13
13
or
14
14
15
15
```
16
-
yarn add @workos-inc/authkit-nextjs
16
+
yarn add @workos-inc/authkit-remix
17
17
```
18
18
19
19
## Pre-flight
@@ -50,57 +50,60 @@ WORKOS_API_PORT=3000 # port to use for API calls
50
50
51
51
### Callback route
52
52
53
-
WorkOS requires that you have a callback URL to redirect users back to after they've authenticated. In your Next.js app, [expose an API route](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) and add the following.
53
+
WorkOS requires that you have a callback URL to redirect users back to after they've authenticated. In your Remix app, [create a new route](https://remix.run/docs/en/main/discussion/routes), name it `callback.tsx` and add the following.
Make sure this route matches the `WORKOS_REDIRECT_URI` variable and the configured redirect URI in your WorkOS dashboard. For instance if your redirect URI is `http://localhost:3000/auth/callback` then you'd put the above code in `/app/auth/callback/route.ts`.
61
+
Make sure this route matches the `WORKOS_REDIRECT_URI` variable and the configured redirect URI in your WorkOS dashboard. For instance if your redirect URI is `http://localhost:3000/auth/callback` then you'd put the above code in `/app/routes/callback.ts`.
62
62
63
-
You can also control the pathname the user will be sent to after signing-in by passing a `returnPathname` option to `handleAuth` like so:
63
+
You can also control the pathname the user will be sent to after signing-in by passing a `returnPathname` option to `authLoader` like so:
64
64
65
65
```ts
66
-
exportconst GET =handleAuth({ returnPathname: '/dashboard' });
67
-
```
68
-
69
-
### Middleware
70
-
71
-
This library relies on [Next.js middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) to provide session management for routes. Put the following in your `middleware.ts` file in the root of your project:
// Get the URL to redirect the user to AuthKit to sign in
99
-
constsignInUrl=awaitgetSignInUrl();
100
-
101
-
// Get the URL to redirect the user to AuthKit to sign up
102
-
constsignUpUrl=awaitgetSignUpUrl();
103
-
104
107
return (
105
108
<>
106
109
<Link href={signInUrl}>Log in</Link>
@@ -110,15 +113,10 @@ export default async function HomePage() {
110
113
}
111
114
112
115
return (
113
-
<form
114
-
action={async () => {
115
-
'use server';
116
-
awaitsignOut();
117
-
}}
118
-
>
116
+
<Form method="post">
119
117
<p>Welcome back {user?.firstName&&`, ${user?.firstName}`}</p>
120
118
<button type="submit">Sign out</button>
121
-
</form>
119
+
</Form>
122
120
);
123
121
}
124
122
```
@@ -128,68 +126,28 @@ export default async function HomePage() {
128
126
For pages where a signed-in user is mandatory, you can use the `ensureSignedIn` option:
129
127
130
128
```jsx
131
-
const { user } = await getUser({ ensureSignedIn: true });
129
+
const { user } = await withAuth({ ensureSignedIn: true });
132
130
```
133
131
134
132
Enabling `ensureSignedIn` will redirect users to AuthKit if they attempt to access the page without being authenticated.
135
133
136
-
### Middleware auth
137
-
138
-
The default behavior ofthis library is to request authentication via the `getUser` method on a per-page basis. There are some use cases where you don't want to call `getUser` (e.g. you don't need user data for your page) or if you'd prefer a "secure by default" approach where every route defined in your middleware matcher is protected unless specified otherwise. In those cases you can opt-in to use middleware auth instead:
139
-
140
-
```ts
141
-
import { authkitMiddleware } from '@workos-inc/authkit-nextjs';
142
-
143
-
export default authkitMiddleware({
144
-
middlewareAuth: {
145
-
enabled: true,
146
-
unauthenticatedPaths: ['/', '/about'],
147
-
},
148
-
});
149
-
150
-
// Match against pages that require auth
151
-
// Leave this out if you want auth on every resource (including images, css etc.)
In the above example the `/admin` page will require a user to be signed in, whereas `/` and `/about` can be accessed without signing in.
156
-
157
-
`unauthenticatedPaths` uses the same glob logic as the [Next.js matcher](https://nextjs.org/docs/pages/building-your-application/routing/middleware#matcher).
158
-
159
134
### Signing out
160
135
161
-
Use the `signOut` method to sign out the current logged in user and redirect to your app's homepage. The homepage redirect is set in your WorkOS dashboard settings under "Redirect".
162
-
163
-
### Visualizing an impersonation
164
-
165
-
Render the `Impersonation` component in your app so that it is clear when someone is [impersonating a user](https://workos.com/docs/user-management/impersonation).
166
-
The component will display a frame with some information about the impersonated user, as well as a button to stop impersonating.
167
-
168
-
```jsx
169
-
import { Impersonation } from '@workos-inc/authkit-nextjs';
170
-
171
-
export default function App() {
172
-
return (
173
-
<div>
174
-
<Impersonation />
175
-
{/* Your app content */}
176
-
</div>
177
-
);
178
-
}
179
-
```
136
+
Use the `signOut` method to sign out the current logged in user, end the session, and redirect to your app's homepage. The homepage redirect is set in your WorkOS dashboard settings under "Redirect".
180
137
181
138
### Get the access token
182
139
183
140
Sometimes it is useful to obtain the access token directly, for instance to make API requests to another service.
184
141
185
142
```jsx
186
-
import { getUser } from '@workos-inc/authkit-nextjs';
143
+
import type { LoaderFunctionArgs, json } from '@remix-run/node';
144
+
import { withAuth } from '@workos-inc/authkit-remix';
187
145
188
-
export default async function HomePage() {
189
-
const { accessToken } = await getUser();
146
+
export async function loader({ request }: LoaderFunctionArgs) {
147
+
const { accessToken } = await withAuth(request);
190
148
191
-
if (!accessToken) {
192
-
return <div>Not signed in</div>;
149
+
if (!accesstoken) {
150
+
// Not signed in
193
151
}
194
152
195
153
const serviceData = await fetch('/api/path', {
@@ -198,22 +156,28 @@ export default async function HomePage() {
198
156
},
199
157
});
200
158
201
-
return <div>{serviceData}</div>;
159
+
return json({
160
+
data: serviceData,
161
+
});
202
162
}
203
163
```
204
164
205
165
### Debugging
206
166
207
-
To enable debug logs, initialize the middleware withthe debug flag enabled.
167
+
To enable debug logs, pass in the debug flag when using `withAuth`.
208
168
209
169
```js
210
-
import { authkitMiddleware } from '@workos-inc/authkit-nextjs';
import { withAuth, getSignInUrl, getSignUpUrl } from '@workos-inc/authkit-remix';
216
171
217
-
#### NEXT_REDIRECT error when using try/catch blocks
172
+
export async function loader({ request }: LoaderFunctionArgs) {
173
+
const { user } = await withAuth(request, {
174
+
debug: true,
175
+
});
218
176
219
-
Wrapping a `getUser({ ensureSignedIn: true })` call in a try/catch block will cause a `NEXT_REDIRECT`error. This is because `getUser` will attempt to redirect the user to AuthKit if no session is detected and redirects in Next must be [called outside a try/catch](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#redirecting).
0 commit comments