Skip to content

Commit 2ab6b6b

Browse files
ph1losofhaydenbleaselvercel[bot]
authored
docs(migrations): fixes middleware configuration for better-auth (#667)
* docs(migrations): fixes middleware configuration for better-auth migration * docs(migrations): adds proper fetch event passing to better-auth middleware * Apply suggestion from @vercel[bot] Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> --------- Co-authored-by: Hayden Bleasel <hello@haydenbleasel.com> Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
1 parent 3ccb143 commit 2ab6b6b

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

docs/content/docs/migrations/authentication/better-auth.mdx

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -202,31 +202,41 @@ export const AuthProvider = ({ children }: AuthProviderProps) => children;
202202

203203
## 7. Change Middleware
204204

205-
Change the middleware in the `auth` package to the following:
205+
Change the middleware in the `auth` package to the following. The middleware checks for a session cookie and redirects unauthenticated users to the sign-in page. The optional `middlewareFn` parameter allows you to add custom logic before the authentication check:
206206

207207
```tsx title="packages/auth/middleware.ts"
208-
import type { NextRequest } from "next/server";
209-
import { NextResponse } from 'next/server';
210-
211-
const isProtectedRoute = (request: NextRequest) => {
212-
return request.url.startsWith("/dashboard"); // change this to your protected route
213-
}
214-
215-
export const authMiddleware = async (request: NextRequest) => {
216-
const url = new URL('/api/auth/get-session', request.nextUrl.origin);
217-
const response = await fetch(url, {
218-
headers: {
219-
cookie: request.headers.get('cookie') || '',
220-
},
221-
});
222-
223-
const session = await response.json();
224-
225-
if (isProtectedRoute(request) && !session) {
226-
return NextResponse.redirect(new URL("/sign-in", request.url));
227-
}
228-
229-
return NextResponse.next();
208+
import { getSessionCookie } from "better-auth/cookies";
209+
import type { NextFetchEvent, NextRequest } from "next/server";
210+
import { NextResponse } from "next/server";
211+
212+
export function authMiddleware(
213+
middlewareFn?: (
214+
_auth: { req: NextRequest; authorized: boolean },
215+
request: NextRequest,
216+
event: NextFetchEvent,
217+
) => Promise<Response> | Response,
218+
) {
219+
return async function middleware(request: NextRequest, event: NextFetchEvent) {
220+
const sessionCookie = getSessionCookie(request);
221+
const authorized = Boolean(sessionCookie);
222+
223+
if (middlewareFn) {
224+
const response = await middlewareFn(
225+
{ req: request, authorized },
226+
request,
227+
event
228+
);
229+
if (response && response.headers.get("Location")) {
230+
return response;
231+
}
232+
}
233+
234+
if (!sessionCookie) {
235+
return NextResponse.redirect(new URL("/sign-in", request.url));
236+
}
237+
238+
return NextResponse.next();
239+
};
230240
}
231241
```
232242

0 commit comments

Comments
 (0)