[GH-ISSUE #1431] Middleware just ain't workin? #8750

Closed
opened 2026-04-13 03:57:04 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Rakhsan on GitHub (Feb 13, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1431

ERROR:
Build Error
Next.js (15.1.7)
Failed to compile

./node_modules/better-auth/dist/react.js
Attempted import error: 'useRef' is not exported from 'react' (imported as 'useRef').

CODE:
import { NextRequest, NextResponse } from "next/server";
import { betterFetch } from "better-auth/react";

const AUTH_ROUTES = [
"/auth/login",
"/auth/register",
"/auth/reset-password",
"/auth/forgot-password",
];

const PRIVATE_ROUTES = [
"/auth/email-verified",
"/settings",
]

export const config = {
matcher: ['/((?!_next|[^?]\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).)'],
}
export default async function authMiddleware(request: NextRequest): Promise {
const pathName = request.nextUrl.pathname;
const { data: session } = await betterFetch(
"/api/auth/get-session", {
baseURL: "http://192.168.0.134:8888",
headers: {
cookie: request.headers.get("cookie") || "",
},
}
)

const isAuthRoutes = AUTH_ROUTES.includes(pathName);
const isPrivateRoutes = PRIVATE_ROUTES.includes(pathName)


if (isAuthRoutes) {
    if (session) {
        return NextResponse.redirect(new URL("/", request.url));
    } else {
        return NextResponse.next();
    }
}

if (!session && isPrivateRoutes) {
    return NextResponse.redirect(new URL("/login", request.url));
}

return NextResponse.next();

}

WHEN I REMOVED MIDDLEWARE WORKED FINE BUT THE MOMENT I ADDED THIS IT JUST DOESN'T WORK. HOW DO I FIX THIS ERROR?

Originally created by @Rakhsan on GitHub (Feb 13, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1431 **ERROR:** Build Error Next.js (15.1.7) Failed to compile ./node_modules/better-auth/dist/react.js Attempted import error: 'useRef' is not exported from 'react' (imported as 'useRef'). **CODE:** import { NextRequest, NextResponse } from "next/server"; import { betterFetch } from "better-auth/react"; const AUTH_ROUTES = [ "/auth/login", "/auth/register", "/auth/reset-password", "/auth/forgot-password", ]; const PRIVATE_ROUTES = [ "/auth/email-verified", "/settings", ] export const config = { matcher: ['/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)'], } export default async function authMiddleware(request: NextRequest): Promise<NextResponse> { const pathName = request.nextUrl.pathname; const { data: session } = await betterFetch<any>( "/api/auth/get-session", { baseURL: "http://192.168.0.134:8888", headers: { cookie: request.headers.get("cookie") || "", }, } ) const isAuthRoutes = AUTH_ROUTES.includes(pathName); const isPrivateRoutes = PRIVATE_ROUTES.includes(pathName) if (isAuthRoutes) { if (session) { return NextResponse.redirect(new URL("/", request.url)); } else { return NextResponse.next(); } } if (!session && isPrivateRoutes) { return NextResponse.redirect(new URL("/login", request.url)); } return NextResponse.next(); } **WHEN I REMOVED MIDDLEWARE WORKED FINE BUT THE MOMENT I ADDED THIS IT JUST DOESN'T WORK. HOW DO I FIX THIS ERROR?**
GiteaMirror added the locked label 2026-04-13 03:57:04 -05:00
Author
Owner

@Rakhsan commented on GitHub (Feb 13, 2025):

fixed it no prob

<!-- gh-comment-id:2657404223 --> @Rakhsan commented on GitHub (Feb 13, 2025): fixed it no prob
Author
Owner

@sovetski commented on GitHub (Apr 5, 2025):

fixed it no prob

good but it is better to share your solution with others

<!-- gh-comment-id:2781036445 --> @sovetski commented on GitHub (Apr 5, 2025): > fixed it no prob good but it is better to share your solution with others
Author
Owner

@soorajkj commented on GitHub (Apr 26, 2025):

fixed it no prob

good but it is better to share your solution with others

Fixed on version 1.2.7. Error was getting empty string as respose if there no session. Now it is returing an object with session: null 👍

<!-- gh-comment-id:2832306934 --> @soorajkj commented on GitHub (Apr 26, 2025): > > fixed it no prob > > good but it is better to share your solution with others Fixed on version 1.2.7. Error was getting empty string as respose if there no session. Now it is returing an object with session: null 👍
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8750