All files / src/app/api/users/me route.ts

100% Statements 25/25
100% Branches 6/6
100% Functions 3/3
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52              5x 5x 5x 2x     3x 3x 2x   1x         5x 5x 5x 1x     4x 3x 3x 3x   1x         13x 13x 13x 2x     11x 11x 10x   1x      
import { NextRequest, NextResponse } from "next/server";
import { getSession } from "@/lib/auth";
import { UserController } from "@/server/api/users";
import { getDb } from "@/lib/db";
import { errorResponse } from "@/server/api/http-errors";
 
export async function GET() {
  try {
    const session = await getSession();
    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
 
    const db = getDb();
    const user = await UserController.fromDb(db).getCurrentUser(session.user.id);
    return NextResponse.json(user);
  } catch (error) {
    return errorResponse(error);
  }
}
 
export async function PATCH(request: NextRequest) {
  try {
    const session = await getSession();
    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
 
    const body = await request.json();
    const db = getDb();
    const user = await UserController.fromDb(db).updateCurrentUser(session.user.id, body);
    return NextResponse.json(user);
  } catch (error) {
    return errorResponse(error);
  }
}
 
export async function DELETE() {
  try {
    const session = await getSession();
    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
 
    const db = getDb();
    await UserController.fromDb(db).deleteCurrentUser(session.user.id);
    return NextResponse.json({ message: "User account deleted" });
  } catch (error) {
    return errorResponse(error);
  }
}