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) {
|
2023-12-31 05:41:55 -05:00
|
|
|
return NextResponse.json({
|
2023-12-30 05:56:40 -05:00
|
|
|
id: exist.id,
|
2023-12-31 05:41:55 -05:00
|
|
|
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,
|
2023-12-31 05:41:55 -05:00
|
|
|
username: newUser.username
|
2023-12-30 05:56:40 -05:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log("[ACCOUNT]", error);
|
|
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
|
|
}
|
|
|
|
}
|