26 lines
756 B
JavaScript
26 lines
756 B
JavaScript
import { NextResponse } from "next/server";
|
|
import dbConnect from "@/lib/mongodb";
|
|
import User from "@/models/User";
|
|
import { verifyToken } from "@/lib/auth";
|
|
|
|
export async function GET(request) {
|
|
try {
|
|
const token = request.headers.get("authorization")?.split(" ")[1];
|
|
if (!token) {
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
}
|
|
|
|
const decoded = verifyToken(token);
|
|
await dbConnect();
|
|
|
|
const user = await User.findById(decoded.userId);
|
|
if (!user) {
|
|
return NextResponse.json({ error: "用户不存在" }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json({ points: user.points });
|
|
} catch (error) {
|
|
return NextResponse.json({ error: "获取积分失败" }, { status: 500 });
|
|
}
|
|
}
|