From a46b2ac3c6c4b81c1dfec55498c2c9f2368b2094 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Mon, 22 May 2023 10:16:09 -0400 Subject: [PATCH] Move to next-auth --- app/api/auth/[...nextauth]/route.ts | 2 ++ app/chat/[id]/page.tsx | 4 +-- app/page.tsx | 4 +-- app/sidebar.tsx | 5 +-- auth.ts | 26 ++++++++++++++ components/ui/login.tsx | 9 +++-- components/ui/user-menu.tsx | 14 ++++---- middleware.ts | 1 + package.json | 8 ++++- pnpm-lock.yaml | 54 ++++++++++++++++++++++++++++- 10 files changed, 107 insertions(+), 20 deletions(-) create mode 100644 app/api/auth/[...nextauth]/route.ts create mode 100644 auth.ts create mode 100644 middleware.ts diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts new file mode 100644 index 0000000..e47b0fd --- /dev/null +++ b/app/api/auth/[...nextauth]/route.ts @@ -0,0 +1,2 @@ +export { GET, POST } from "@/auth"; +export const runtime = "edge"; diff --git a/app/chat/[id]/page.tsx b/app/chat/[id]/page.tsx index 4129e8c..f4e6f06 100644 --- a/app/chat/[id]/page.tsx +++ b/app/chat/[id]/page.tsx @@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma"; import { Chat } from "../../chat"; import { type Metadata } from "next"; -import { getServerSession } from "@/lib/session/get-server-session"; +import { auth } from "@/auth"; export interface ChatPageProps { params: { @@ -29,7 +29,7 @@ export const runtime = "nodejs"; // default export const preferredRegion = "home"; export const dynamic = "force-dynamic"; export default async function ChatPage({ params }: ChatPageProps) { - const session = await getServerSession(); + const session = await auth(); const chat = await prisma.chat.findFirst({ where: { id: params.id, diff --git a/app/page.tsx b/app/page.tsx index a3ca75a..7dec5b3 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,6 +1,6 @@ -import { getServerSession } from "@/lib/session/get-server-session"; import { Chat } from "./chat"; import { Sidebar } from "./sidebar"; +import { auth } from "@/auth"; // Prisma does not support Edge without the Data Proxy currently export const runtime = "nodejs"; // default @@ -8,7 +8,7 @@ export const preferredRegion = "home"; export const dynamic = "force-dynamic"; export default async function IndexPage() { - const session = await getServerSession(); + const session = await auth(); return (
diff --git a/app/sidebar.tsx b/app/sidebar.tsx index e2379f3..d43cd82 100644 --- a/app/sidebar.tsx +++ b/app/sidebar.tsx @@ -2,7 +2,7 @@ import { NextChatLogo } from "@/components/ui/nextchat-logo"; import { Login } from "@/components/ui/login"; import { UserMenu } from "@/components/ui/user-menu"; import { prisma } from "@/lib/prisma"; -import { type Session } from "@/lib/session/types"; +import { type Session } from "@auth/nextjs/types"; import { cn } from "@/lib/utils"; import { Plus } from "lucide-react"; import Link from "next/link"; @@ -70,7 +70,8 @@ async function SidebarList({ session }: { session?: Session }) { updatedAt: "desc", }, }), - [session?.user.id || ""], + // @ts-ignore + [session?.user?.id || ""], { revalidate: 3600, } diff --git a/auth.ts b/auth.ts new file mode 100644 index 0000000..100244e --- /dev/null +++ b/auth.ts @@ -0,0 +1,26 @@ +import NextAuth from "@auth/nextjs"; +import GitHub from "@auth/nextjs/providers/github"; +import { NextResponse } from "next/server"; +export const { + handlers: { GET, POST }, + auth, + CSRF_experimental, +} = NextAuth({ + // @ts-ignore + providers: [GitHub], + session: { strategy: "jwt" }, + async authorized({ request, auth }: any) { + const url = request.nextUrl; + + if (request.method === "POST") { + const { authToken } = (await request.json()) ?? {}; + // If the request has a valid auth token, it is authorized + const valid = true; + if (valid) return true; + return NextResponse.json("Invalid auth token", { status: 401 }); + } + + // Logged in users are authenticated, otherwise redirect to login page + return !!auth; + }, +}); diff --git a/components/ui/login.tsx b/components/ui/login.tsx index f0c37f4..07edb90 100644 --- a/components/ui/login.tsx +++ b/components/ui/login.tsx @@ -1,14 +1,13 @@ "use client"; - -import Link from "next/link"; +import { signIn } from "@auth/nextjs/client"; export function Login() { return ( - signIn("github")} > Login - + ); } diff --git a/components/ui/user-menu.tsx b/components/ui/user-menu.tsx index 97ff9b4..fb32743 100644 --- a/components/ui/user-menu.tsx +++ b/components/ui/user-menu.tsx @@ -1,7 +1,9 @@ "use client"; import { ThemeToggle } from "@/components/theme-toggle"; -import { type Session } from "@/lib/session/types"; +import { type Session } from "@auth/nextjs/types"; import * as DropdownMenu from "@radix-ui/react-dropdown-menu"; +import { signOut } from "@auth/nextjs/client"; + import Image from "next/image"; export interface UserMenuProps { @@ -14,12 +16,12 @@ export function UserMenu({ session }: UserMenuProps) {