Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

651 B
Raw Blame
import { eq } from "drizzle-orm";

import { db as globalDb } from "@karakeep/db";
import { users } from "@karakeep/db/schema";

import { AuthedContext } from "..";

export async function buildImpersonatingAuthedContext(
  userId: string,
  db: AuthedContext["db"] = globalDb,
): Promise<AuthedContext> {
  const user = await db.query.users.findFirst({
    where: eq(users.id, userId),
  });
  if (!user) {
    throw new Error("User not found");
  }

  return {
    user: {
      id: user.id,
      name: user.name,
      email: user.email,
      role: user.role,
    },
    auth: { type: "internal" },
    db,
    req: {
      ip: null,
    },
  };
}