mirror of
https://github.com/beilunyang/moemail.git
synced 2026-09-05 15:26:38 +08:00
fix(api): support API Key auth for send endpoint
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { NextResponse } from "next/server"
|
import { NextResponse } from "next/server"
|
||||||
import { auth } from "@/lib/auth"
|
import { getUserId } from "@/lib/apiKey"
|
||||||
import { createDb } from "@/lib/db"
|
import { createDb } from "@/lib/db"
|
||||||
import { emails, messages } from "@/lib/schema"
|
import { emails, messages } from "@/lib/schema"
|
||||||
import { eq } from "drizzle-orm"
|
import { eq } from "drizzle-orm"
|
||||||
@@ -49,8 +49,8 @@ export async function POST(
|
|||||||
{ params }: { params: Promise<{ id: string }> }
|
{ params }: { params: Promise<{ id: string }> }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const session = await auth()
|
const userId = await getUserId()
|
||||||
if (!session?.user?.id) {
|
if (!userId) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "未授权" },
|
{ error: "未授权" },
|
||||||
{ status: 401 }
|
{ status: 401 }
|
||||||
@@ -60,7 +60,7 @@ export async function POST(
|
|||||||
const { id } = await params
|
const { id } = await params
|
||||||
const db = createDb()
|
const db = createDb()
|
||||||
|
|
||||||
const permissionResult = await checkSendPermission(session.user.id)
|
const permissionResult = await checkSendPermission(userId)
|
||||||
if (!permissionResult.canSend) {
|
if (!permissionResult.canSend) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: permissionResult.error },
|
{ error: permissionResult.error },
|
||||||
@@ -90,7 +90,7 @@ export async function POST(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (email.userId !== session.user.id) {
|
if (email.userId !== userId) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "无权访问此邮箱" },
|
{ error: "无权访问此邮箱" },
|
||||||
{ status: 403 }
|
{ status: 403 }
|
||||||
|
|||||||
Reference in New Issue
Block a user