67 lines
1.8 KiB
TypeScript
67 lines
1.8 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) {
|
|
// const apikey = await db.apiKey.findFirst({
|
|
// where: {
|
|
// userId: user.toLowerCase() as string
|
|
// }
|
|
// })
|
|
return {
|
|
id: exist.id,
|
|
username: exist.username,
|
|
//key: apikey?.id as string
|
|
};
|
|
}
|
|
|
|
const newUser = await db.user.create({
|
|
data: {
|
|
username: user.toLowerCase() as string,
|
|
}
|
|
});
|
|
|
|
// const apikey = await db.apiKey.create({
|
|
// data: {
|
|
// id: generateToken(),
|
|
// label: "Default",
|
|
// userId: user.toLowerCase() as string
|
|
// }
|
|
// })
|
|
|
|
return NextResponse.json({
|
|
id: newUser.id,
|
|
username: newUser.username,
|
|
//key: apikey.id
|
|
});
|
|
} catch (error) {
|
|
console.log("[ACCOUNT]", error);
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
}
|
|
} |