Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generate a random secret: https://generate-secret.vercel.app/32 or `openssl rand -base64 32`
AUTH_SECRET=****
BETTER_AUTH_SECRET=****
BETTER_AUTH_URL=****

# The following keys below are automatically created and
# added to your environment when you deploy on Vercel
Expand Down
37 changes: 20 additions & 17 deletions app/(auth)/actions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"use server";

import { z } from "zod";

import { createUser, getUser } from "@/lib/db/queries";

import { signIn } from "./auth";
import { auth } from "@/lib/auth";

const authFormSchema = z.object({
email: z.string().email(),
Expand All @@ -25,10 +22,11 @@ export const login = async (
password: formData.get("password"),
});

await signIn("credentials", {
email: validatedData.email,
password: validatedData.password,
redirect: false,
await auth.api.signInEmail({
body: {
email: validatedData.email,
password: validatedData.password,
},
});

return { status: "success" };
Expand Down Expand Up @@ -61,24 +59,29 @@ export const register = async (
password: formData.get("password"),
});

const [user] = await getUser(validatedData.email);
const result = await auth.api.signUpEmail({
body: {
email: validatedData.email,
password: validatedData.password,
name: validatedData.email,
},
});

if (user) {
return { status: "user_exists" } as RegisterActionState;
if (!result) {
return { status: "failed" };
}
await createUser(validatedData.email, validatedData.password);
await signIn("credentials", {
email: validatedData.email,
password: validatedData.password,
redirect: false,
});

return { status: "success" };
} catch (error) {
if (error instanceof z.ZodError) {
return { status: "invalid_data" };
}

const message = error instanceof Error ? error.message : "";
if (message.includes("already exists") || message.includes("UNIQUE")) {
return { status: "user_exists" };
}

return { status: "failed" };
}
};
1 change: 0 additions & 1 deletion app/(auth)/api/auth/[...nextauth]/route.ts

This file was deleted.

21 changes: 0 additions & 21 deletions app/(auth)/api/auth/guest/route.ts

This file was deleted.

14 changes: 0 additions & 14 deletions app/(auth)/auth.config.ts

This file was deleted.

94 changes: 0 additions & 94 deletions app/(auth)/auth.ts

This file was deleted.

8 changes: 4 additions & 4 deletions app/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import Link from "next/link";
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import { useActionState, useEffect, useState } from "react";

import { AuthForm } from "@/components/auth-form";
import { SubmitButton } from "@/components/submit-button";
import { toast } from "@/components/toast";
import { useSession } from "@/lib/client";
import { type LoginActionState, login } from "../actions";

export default function Page() {
Expand All @@ -23,9 +23,9 @@ export default function Page() {
}
);

const { update: updateSession } = useSession();
const { refetch } = useSession();

// biome-ignore lint/correctness/useExhaustiveDependencies: router and updateSession are stable refs
// biome-ignore lint/correctness/useExhaustiveDependencies: router and refetch are stable refs
useEffect(() => {
if (state.status === "failed") {
toast({
Expand All @@ -39,7 +39,7 @@ export default function Page() {
});
} else if (state.status === "success") {
setIsSuccessful(true);
updateSession();
refetch();
router.refresh();
}
}, [state.status]);
Expand Down
8 changes: 4 additions & 4 deletions app/(auth)/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import Link from "next/link";
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import { useActionState, useEffect, useState } from "react";
import { AuthForm } from "@/components/auth-form";
import { SubmitButton } from "@/components/submit-button";
import { toast } from "@/components/toast";
import { useSession } from "@/lib/client";
import { type RegisterActionState, register } from "../actions";

export default function Page() {
Expand All @@ -22,9 +22,9 @@ export default function Page() {
}
);

const { update: updateSession } = useSession();
const { refetch } = useSession();

// biome-ignore lint/correctness/useExhaustiveDependencies: router and updateSession are stable refs
// biome-ignore lint/correctness/useExhaustiveDependencies: router and refetch are stable refs
useEffect(() => {
if (state.status === "user_exists") {
toast({ type: "error", description: "Account already exists!" });
Expand All @@ -39,7 +39,7 @@ export default function Page() {
toast({ type: "success", description: "Account created successfully!" });

setIsSuccessful(true);
updateSession();
refetch();
router.refresh();
}
}, [state.status]);
Expand Down
4 changes: 4 additions & 0 deletions app/(chat)/api/auth/[...all]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { toNextJsHandler } from "better-auth/next-js";
import { auth } from "@/lib/auth";

export const { GET, POST } = toNextJsHandler(auth);
8 changes: 4 additions & 4 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -67,7 +67,7 @@ export async function POST(request: Request) {

const [, session] = await Promise.all([
checkBotId().catch(() => null),
auth(),
getSession(),
]);

if (!session?.user) {
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions app/(chat)/api/document/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { auth } from "@/app/(auth)/auth";
import type { ArtifactKind } from "@/components/artifact";
import { getSession } from "@/lib/auth";
import {
deleteDocumentsByIdAfterTimestamp,
getDocumentsById,
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions app/(chat)/api/files/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 });
Expand Down
Loading
Loading