chore: simplify next-auth (#80)

This commit is contained in:
Balázs Orbán 2023-06-27 20:03:01 +02:00 committed by GitHub
parent 5912336152
commit 012ea40414
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 116 deletions

View file

@ -1 +1,2 @@
export { GET, POST } from '@/auth'
export const runtime = 'edge'

View file

@ -16,9 +16,9 @@ const openai = new OpenAIApi(configuration)
export async function POST(req: Request) {
const json = await req.json()
const { messages, previewToken } = json
const session = await auth()
const userId = (await auth())?.user.id
if (session == null) {
if (!userId) {
return new Response('Unauthorized', {
status: 401
})
@ -38,31 +38,28 @@ export async function POST(req: Request) {
const stream = OpenAIStream(res, {
async onCompletion(completion) {
const title = json.messages[0].content.substring(0, 100)
const userId = session?.user?.id
if (userId) {
const id = json.id ?? nanoid()
const createdAt = Date.now()
const path = `/chat/${id}`
const payload = {
id,
title,
userId,
createdAt,
path,
messages: [
...messages,
{
content: completion,
role: 'assistant'
}
]
}
await kv.hmset(`chat:${id}`, payload)
await kv.zadd(`user:chat:${userId}`, {
score: createdAt,
member: `chat:${id}`
})
const id = json.id ?? nanoid()
const createdAt = Date.now()
const path = `/chat/${id}`
const payload = {
id,
title,
userId,
createdAt,
path,
messages: [
...messages,
{
content: completion,
role: 'assistant'
}
]
}
await kv.hmset(`chat:${id}`, payload)
await kv.zadd(`user:chat:${userId}`, {
score: createdAt,
member: `chat:${id}`
})
}
})