Update next-auth

This commit is contained in:
Jared Palmer 2023-06-16 12:03:09 -04:00
parent 187b55aad7
commit fef709a21d
6 changed files with 28 additions and 10 deletions

View file

@ -44,7 +44,7 @@ export async function getChat(id: string, userId: string) {
} }
export async function removeChat({ id, path }: { id: string; path: string }) { export async function removeChat({ id, path }: { id: string; path: string }) {
const session = await auth<{ stuff: string }>() const session = await auth()
if (!session) { if (!session) {
throw new Error('Unauthorized') throw new Error('Unauthorized')

View file

@ -1,9 +1,8 @@
import { kv } from '@vercel/kv' import { kv } from '@vercel/kv'
import { OpenAIStream, StreamingTextResponse } from 'ai-connector' import { OpenAIStream, StreamingTextResponse } from 'ai-connector'
import { Configuration, OpenAIApi } from 'openai-edge' import { Configuration, OpenAIApi } from 'openai-edge'
import { nanoid } from '@/lib/utils' import { nanoid } from '@/lib/utils'
import { currentUser } from '@clerk/nextjs' import { auth } from '@/auth'
export const runtime = 'edge' export const runtime = 'edge'
@ -18,8 +17,8 @@ if (!process.env.OPENAI_API_KEY) {
} }
export async function POST(req: Request) { export async function POST(req: Request) {
const user = await currentUser() const session = await auth()
if (user == null) { if (session == null) {
return new Response('Unauthorized', { status: 401 }) return new Response('Unauthorized', { status: 401 })
} }
@ -40,7 +39,7 @@ export async function POST(req: Request) {
const stream = OpenAIStream(res, { const stream = OpenAIStream(res, {
async onCompletion(completion) { async onCompletion(completion) {
const title = json.messages[0].content.substring(0, 100) const title = json.messages[0].content.substring(0, 100)
const userId = user.id const userId = session.user.id
const id = json.id ?? nanoid() const id = json.id ?? nanoid()
const createdAt = Date.now() const createdAt = Date.now()
const path = `/chat/${id}` const path = `/chat/${id}`

View file

@ -1,5 +1,5 @@
import { type Metadata } from 'next' import { type Metadata } from 'next'
import { auth } from '@clerk/nextjs' import { auth } from '@/auth'
import { Chat } from '@/components/chat' import { Chat } from '@/components/chat'
import { getChat } from '@/app/actions' import { getChat } from '@/app/actions'

View file

@ -5,16 +5,23 @@ export const {
handlers: { GET, POST }, handlers: { GET, POST },
auth, auth,
CSRF_experimental CSRF_experimental
// @ts-ignore
} = NextAuth({ } = NextAuth({
// @ts-ignore // @ts-ignore
providers: [GitHub], providers: [GitHub],
callbacks: { callbacks: {
// @ts-ignore
jwt: async ({ token, profile }) => { jwt: async ({ token, profile }) => {
if (profile?.id) { if (profile?.id) {
token.id = profile.id token.id = profile.id
token.image = profile.picture token.image = profile.picture
} }
return token return token
},
// @ts-ignore
authorized({ request, auth }) {
if (!request.nextUrl.pathname.startsWith('/share/')) return true
return !!auth?.user
} }
}, },
pages: { pages: {

10
next-auth.d.ts vendored
View file

@ -1,7 +1,13 @@
import NextAuth, { DefaultSession } from 'next-auth'
declare module 'next-auth' { declare module 'next-auth' {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session { interface Session {
user: { user: {
id: string /** The user's postal address. */
} address: string
} & DefaultSession['user']
} }
} }

View file

@ -24,6 +24,12 @@
], ],
"strictNullChecks": true "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"] "exclude": ["node_modules"]
} }