chore: simplify next-auth (#80)
This commit is contained in:
parent
5912336152
commit
012ea40414
8 changed files with 55 additions and 116 deletions
|
|
@ -1 +1,2 @@
|
|||
export { GET, POST } from '@/auth'
|
||||
export const runtime = 'edge'
|
||||
|
|
|
|||
|
|
@ -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}`
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue