hermes-web/app/api/account/route.ts

51 lines
1.2 KiB
TypeScript

import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import { auth } from "@/auth";
import fetchUser from "@/lib/fetch-user";
export async function GET(req: Request) {
try {
return NextResponse.json(await fetchUser(req))
} catch (error) {
console.log("[ACCOUNT]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
export async function POST(req: Request) {
try {
const session = await auth()
const user = session?.user?.name
if (!user) {
return new NextResponse("Internal Error", { status: 401 })
}
const exist = await db.user.findFirst({
where: {
name: user
}
});
if (exist) {
return NextResponse.json({
id: exist.id,
username: exist.name
});
}
const newUser = await db.user.create({
data: {
name: user,
}
});
return NextResponse.json({
id: newUser.id,
username: newUser.name
});
} catch (error) {
console.log("[ACCOUNT]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}