51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { db } from "@/lib/db"
|
|
import { NextResponse } from "next/server";
|
|
import { auth } from "@/auth";
|
|
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 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 });
|
|
}
|
|
} |