52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
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({
|
|
id: exist.id,
|
|
username: exist.username
|
|
});
|
|
}
|
|
|
|
const newUser = await db.user.create({
|
|
data: {
|
|
username: user.toLowerCase() as string,
|
|
}
|
|
});
|
|
|
|
return NextResponse.json({
|
|
id: newUser.id,
|
|
username: newUser.username
|
|
});
|
|
} catch (error) {
|
|
console.log("[ACCOUNT]", error);
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
}
|
|
} |