Better Auth with T3 Stack and Middleware not working

Hey i'm using T3 stack and trying to implement better auth to it. I'm having problems with my middleware, as it seems that it does not do anything. middleware.ts:
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);

if (!sessionCookie) {
return NextResponse.redirect(new URL("/", request.url));
}

return NextResponse.next();
}

export const config = {
matcher: ["/dashboard/:path*"],
};`
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);

if (!sessionCookie) {
return NextResponse.redirect(new URL("/", request.url));
}

return NextResponse.next();
}

export const config = {
matcher: ["/dashboard/:path*"],
};`
routers/story.ts:
getAll: protectedProcedure.query(async ({ ctx }) => {
return ctx.db.query.story.findMany({
where: (s, { eq }) => eq(s.userId, ctx.session.user.id),
orderBy: (s, { desc }) => [desc(s.createdAt)],
with: {
child: true,
},
});
}),
getAll: protectedProcedure.query(async ({ ctx }) => {
return ctx.db.query.story.findMany({
where: (s, { eq }) => eq(s.userId, ctx.session.user.id),
orderBy: (s, { desc }) => [desc(s.createdAt)],
with: {
child: true,
},
});
}),
lib/auth.ts:
getAll: protectedProcedure.query(async ({ ctx }) => {
return ctx.db.query.story.findMany({
where: (s, { eq }) => eq(s.userId, ctx.session.user.id),
orderBy: (s, { desc }) => [desc(s.createdAt)],
with: {
child: true,
},
});
}),
getAll: protectedProcedure.query(async ({ ctx }) => {
return ctx.db.query.story.findMany({
where: (s, { eq }) => eq(s.userId, ctx.session.user.id),
orderBy: (s, { desc }) => [desc(s.createdAt)],
with: {
child: true,
},
});
}),
lib/auth-client.ts:
import { createAuthClient } from "better-auth/react"

export const authClient = createAuthClient({
baseURL: "http://localhost:3000"
})

export const { signIn, signUp, useSession, signOut } = authClient;
import { createAuthClient } from "better-auth/react"

export const authClient = createAuthClient({
baseURL: "http://localhost:3000"
})

export const { signIn, signUp, useSession, signOut } = authClient;
dashboardPage:
export default function DashboardPage() {
const { data: stories, isLoading, error } = api.story.getAll.useQuery();
const { filteredStories, counts } = useStoryFilters({
stories: stories ?? [],
searchQuery,
activeFilter,
});

return (
...
)
export default function DashboardPage() {
const { data: stories, isLoading, error } = api.story.getAll.useQuery();
const { filteredStories, counts } = useStoryFilters({
stories: stories ?? [],
searchQuery,
activeFilter,
});

return (
...
)
What is causing this middleware problem? Middleawre is at the root, same place where package.json is. package.json:
"next": "^15.2.3",
"next": "^15.2.3",
2 Replies
Ping
Ping3mo ago
what do you mean the middleware does not do anything?
Manqo
ManqoOP3mo ago
I'm able to Access localhost:/Dashboard i thought middleware would block me.

Did you find this page helpful?