From fef709a21d2e14b17c81c1a2ba3929adbf546dfe Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Fri, 16 Jun 2023 12:03:09 -0400 Subject: [PATCH] Update next-auth --- app/actions.ts | 2 +- app/api/chat/route.ts | 9 ++++----- app/chat/[id]/page.tsx | 2 +- auth.ts | 7 +++++++ next-auth.d.ts | 10 ++++++++-- tsconfig.json | 8 +++++++- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/actions.ts b/app/actions.ts index 4ba7b6e..451da5d 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -44,7 +44,7 @@ export async function getChat(id: string, userId: string) { } export async function removeChat({ id, path }: { id: string; path: string }) { - const session = await auth<{ stuff: string }>() + const session = await auth() if (!session) { throw new Error('Unauthorized') diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 1d4af1a..58008c3 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -1,9 +1,8 @@ import { kv } from '@vercel/kv' import { OpenAIStream, StreamingTextResponse } from 'ai-connector' import { Configuration, OpenAIApi } from 'openai-edge' - import { nanoid } from '@/lib/utils' -import { currentUser } from '@clerk/nextjs' +import { auth } from '@/auth' export const runtime = 'edge' @@ -18,8 +17,8 @@ if (!process.env.OPENAI_API_KEY) { } export async function POST(req: Request) { - const user = await currentUser() - if (user == null) { + const session = await auth() + if (session == null) { return new Response('Unauthorized', { status: 401 }) } @@ -40,7 +39,7 @@ export async function POST(req: Request) { const stream = OpenAIStream(res, { async onCompletion(completion) { const title = json.messages[0].content.substring(0, 100) - const userId = user.id + const userId = session.user.id const id = json.id ?? nanoid() const createdAt = Date.now() const path = `/chat/${id}` diff --git a/app/chat/[id]/page.tsx b/app/chat/[id]/page.tsx index c856cb0..a3504bc 100644 --- a/app/chat/[id]/page.tsx +++ b/app/chat/[id]/page.tsx @@ -1,5 +1,5 @@ import { type Metadata } from 'next' -import { auth } from '@clerk/nextjs' +import { auth } from '@/auth' import { Chat } from '@/components/chat' import { getChat } from '@/app/actions' diff --git a/auth.ts b/auth.ts index 18fba36..5d60d94 100644 --- a/auth.ts +++ b/auth.ts @@ -5,16 +5,23 @@ export const { handlers: { GET, POST }, auth, CSRF_experimental + // @ts-ignore } = NextAuth({ // @ts-ignore providers: [GitHub], callbacks: { + // @ts-ignore jwt: async ({ token, profile }) => { if (profile?.id) { token.id = profile.id token.image = profile.picture } return token + }, + // @ts-ignore + authorized({ request, auth }) { + if (!request.nextUrl.pathname.startsWith('/share/')) return true + return !!auth?.user } }, pages: { diff --git a/next-auth.d.ts b/next-auth.d.ts index 86936f6..5b3f52b 100644 --- a/next-auth.d.ts +++ b/next-auth.d.ts @@ -1,7 +1,13 @@ +import NextAuth, { DefaultSession } from 'next-auth' + declare module 'next-auth' { + /** + * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context + */ interface Session { user: { - id: string - } + /** The user's postal address. */ + address: string + } & DefaultSession['user'] } } diff --git a/tsconfig.json b/tsconfig.json index 1b03bc2..9f91b9b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,6 +24,12 @@ ], "strictNullChecks": true }, - "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], + "include": [ + "next-env.d.ts", + "next-auth.d.ts", + "**/*.ts", + "**/*.tsx", + ".next/types/**/*.ts" + ], "exclude": ["node_modules"] }