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

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-12-30 05:56:40 -05:00
import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { generateToken } from "../token/route";
import fetchUserUsingAPI from "@/lib/validate-api";
export async function GET(req: Request) {
try {
return NextResponse.json(await fetchUserUsingAPI(req))
} catch (error) {
console.log("[ACCOUNT]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
export async function POST(req: Request) {
try {
const session = await getServerSession()
const user = session?.user?.name
if (!user) {
return new NextResponse("Internal Error", { status: 401 })
}
const exist = await db.user.findFirst({
where: {
username: user.toLowerCase() as string
}
});
if (exist) {
return NextResponse.json({
2023-12-30 05:56:40 -05:00
id: exist.id,
username: exist.username
});
2023-12-30 05:56:40 -05:00
}
const newUser = await db.user.create({
data: {
username: user.toLowerCase() as string,
}
});
return NextResponse.json({
id: newUser.id,
username: newUser.username
2023-12-30 05:56:40 -05:00
});
} catch (error) {
console.log("[ACCOUNT]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}