fix(auth): migrate from next-auth to better-auth (#1453)

This commit is contained in:
dancer 2026-03-13 23:18:01 +00:00 committed by GitHub
parent 453f5bb3e6
commit b4f595a68c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 668 additions and 390 deletions

View file

@ -0,0 +1,4 @@
import { toNextJsHandler } from "better-auth/next-js";
import { auth } from "@/lib/auth";
export const { GET, POST } = toNextJsHandler(auth);

View file

@ -10,7 +10,6 @@ import {
import { checkBotId } from "botid/server";
import { after } from "next/server";
import { createResumableStreamContext } from "resumable-stream";
import { auth, type UserType } from "@/app/(auth)/auth";
import { entitlementsByUserType } from "@/lib/ai/entitlements";
import { allowedModelIds } from "@/lib/ai/models";
import { type RequestHints, systemPrompt } from "@/lib/ai/prompts";
@ -19,6 +18,7 @@ import { createDocument } from "@/lib/ai/tools/create-document";
import { getWeather } from "@/lib/ai/tools/get-weather";
import { requestSuggestions } from "@/lib/ai/tools/request-suggestions";
import { updateDocument } from "@/lib/ai/tools/update-document";
import { getSession, getUserType, type UserType } from "@/lib/auth";
import { isProductionEnvironment } from "@/lib/constants";
import {
createStreamId,
@ -67,7 +67,7 @@ export async function POST(request: Request) {
const [, session] = await Promise.all([
checkBotId().catch(() => null),
auth(),
getSession(),
]);
if (!session?.user) {
@ -80,7 +80,7 @@ export async function POST(request: Request) {
await checkIpRateLimit(ipAddress(request));
const userType: UserType = session.user.type;
const userType: UserType = getUserType(session.user);
const messageCount = await getMessageCountByUserId({
id: session.user.id,
@ -295,7 +295,7 @@ export async function DELETE(request: Request) {
return new ChatbotError("bad_request:api").toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:chat").toResponse();

View file

@ -1,5 +1,5 @@
import { auth } from "@/app/(auth)/auth";
import type { ArtifactKind } from "@/components/artifact";
import { getSession } from "@/lib/auth";
import {
deleteDocumentsByIdAfterTimestamp,
getDocumentsById,
@ -18,7 +18,7 @@ export async function GET(request: Request) {
).toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:document").toResponse();
@ -50,7 +50,7 @@ export async function POST(request: Request) {
).toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("not_found:document").toResponse();
@ -103,7 +103,7 @@ export async function DELETE(request: Request) {
).toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:document").toResponse();

View file

@ -2,7 +2,7 @@ import { put } from "@vercel/blob";
import { NextResponse } from "next/server";
import { z } from "zod";
import { auth } from "@/app/(auth)/auth";
import { getSession } from "@/lib/auth";
// Use Blob instead of File since File is not available in Node.js environment
const FileSchema = z.object({
@ -18,7 +18,7 @@ const FileSchema = z.object({
});
export async function POST(request: Request) {
const session = await auth();
const session = await getSession();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

View file

@ -1,5 +1,5 @@
import type { NextRequest } from "next/server";
import { auth } from "@/app/(auth)/auth";
import { getSession } from "@/lib/auth";
import { deleteAllChatsByUserId, getChatsByUserId } from "@/lib/db/queries";
import { ChatbotError } from "@/lib/errors";
@ -17,7 +17,7 @@ export async function GET(request: NextRequest) {
).toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:chat").toResponse();
@ -34,7 +34,7 @@ export async function GET(request: NextRequest) {
}
export async function DELETE() {
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:chat").toResponse();

View file

@ -1,4 +1,4 @@
import { auth } from "@/app/(auth)/auth";
import { getSession } from "@/lib/auth";
import { getSuggestionsByDocumentId } from "@/lib/db/queries";
import { ChatbotError } from "@/lib/errors";
@ -13,7 +13,7 @@ export async function GET(request: Request) {
).toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:suggestions").toResponse();

View file

@ -1,4 +1,4 @@
import { auth } from "@/app/(auth)/auth";
import { getSession } from "@/lib/auth";
import { getChatById, getVotesByChatId, voteMessage } from "@/lib/db/queries";
import { ChatbotError } from "@/lib/errors";
@ -13,7 +13,7 @@ export async function GET(request: Request) {
).toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:vote").toResponse();
@ -49,7 +49,7 @@ export async function PATCH(request: Request) {
).toResponse();
}
const session = await auth();
const session = await getSession();
if (!session?.user) {
return new ChatbotError("unauthorized:vote").toResponse();