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

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-12-30 05:56:40 -05:00
import { db } from "@/lib/db"
import { NextResponse } from "next/server";
2024-01-02 02:26:20 -05:00
import { auth } from "@/auth";
2024-01-04 16:57:32 -05:00
import fetchUser from "@/lib/fetch-user";
2023-12-30 05:56:40 -05:00
export async function GET(req: Request) {
try {
const user = await fetchUser(req)
if (!user) return new NextResponse("Internal Error", { status: 401 })
return NextResponse.json(user)
2023-12-30 05:56:40 -05:00
} catch (error) {
console.log("[ACCOUNT]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
export async function POST(req: Request) {
try {
2024-01-02 02:26:20 -05:00
const session = await auth()
2023-12-30 05:56:40 -05:00
const user = session?.user?.name
if (!user) {
return new NextResponse("Internal Error", { status: 401 })
}
const exist = await db.user.findFirst({
where: {
2024-01-02 02:26:20 -05:00
name: user
2023-12-30 05:56:40 -05:00
}
});
if (exist) {
return NextResponse.json({
2023-12-30 05:56:40 -05:00
id: exist.id,
2024-01-02 02:26:20 -05:00
username: exist.name
});
2023-12-30 05:56:40 -05:00
}
const newUser = await db.user.create({
data: {
2024-01-02 02:26:20 -05:00
name: user,
2023-12-30 05:56:40 -05:00
}
});
return NextResponse.json({
id: newUser.id,
2024-01-02 02:26:20 -05:00
username: newUser.name
2023-12-30 05:56:40 -05:00
});
} catch (error) {
console.log("[ACCOUNT]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}