jeffreyyvdb
jeffreyyvdb
BABetter Auth
Created by jeffreyyvdb on 4/20/2025 in #help
Better auth does not work on NextJS
2025-04-20T19:46:49.619Z WARN [Better Auth]: No database configuration provided. Using memory adapter in development This warning made me think it is possible to work with the in memory adapter
28 replies
BABetter Auth
Created by jeffreyyvdb on 4/20/2025 in #help
Better auth does not work on NextJS
Okay thank you, but does not seem to be the solution to why I don't have a session in my dashboard page (server side)
28 replies
BABetter Auth
Created by jeffreyyvdb on 4/20/2025 in #help
Better auth does not work on NextJS
No description
28 replies
BABetter Auth
Created by jeffreyyvdb on 4/20/2025 in #help
Better auth does not work on NextJS
true, i changed it to be more logical, but the issues still exists
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

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

if (!cookies) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
return NextResponse.next();
}

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

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

if (!cookies) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
return NextResponse.next();
}

export const config = {
matcher: ["/dashboard"],
};
28 replies
BABetter Auth
Created by jeffreyyvdb on 4/20/2025 in #help
Better auth does not work on NextJS
Do i miss anything?
28 replies
BABetter Auth
Created by jeffreyyvdb on 4/20/2025 in #help
Better auth does not work on NextJS
Yes this is my auth config, i also have a auth-client, middleware and a api route for auth. auth.ts:
typescript
import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";

export const auth = betterAuth({
appName: "Better Auth Demo",
socialProviders: {
google: {
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
},
},
plugins: [nextCookies()],
});
typescript
import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";

export const auth = betterAuth({
appName: "Better Auth Demo",
socialProviders: {
google: {
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
},
},
plugins: [nextCookies()],
});
auth-client.ts:
typescript
import { createAuthClient } from "better-auth/react";

import { toast } from "sonner";

export const client = createAuthClient({
fetchOptions: {
onError(e) {
if (e.error.status === 429) {
toast.error("Too many requests. Please try again later.");
}
},
},
});

export const { signUp, signIn, signOut, useSession } = client;

client.$store.listen("$sessionSignal", async () => {});
typescript
import { createAuthClient } from "better-auth/react";

import { toast } from "sonner";

export const client = createAuthClient({
fetchOptions: {
onError(e) {
if (e.error.status === 429) {
toast.error("Too many requests. Please try again later.");
}
},
},
});

export const { signUp, signIn, signOut, useSession } = client;

client.$store.listen("$sessionSignal", async () => {});
Middleware
typescript
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

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

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

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

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

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

export const config = {
matcher: ["/"],
};
api/auth/[...all]/route.ts:
typescript
import { auth } from "@/lib/auth"; // path to your auth file
import { toNextJsHandler } from "better-auth/next-js";

export const { POST, GET } = toNextJsHandler(auth);
typescript
import { auth } from "@/lib/auth"; // path to your auth file
import { toNextJsHandler } from "better-auth/next-js";

export const { POST, GET } = toNextJsHandler(auth);
28 replies
BABetter Auth
Created by jeffreyyvdb on 4/20/2025 in #help
Better auth does not work on NextJS
@daveycodez Yes, i added it like this but does not seem to help
import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";

export const auth = betterAuth({
appName: "Better Auth Demo",
socialProviders: {
google: {
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
},
},
plugins: [nextCookies()],
});
import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";

export const auth = betterAuth({
appName: "Better Auth Demo",
socialProviders: {
google: {
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
},
},
plugins: [nextCookies()],
});
28 replies