Files
webstudio/apps/builder/app/shared/db/user.server.ts
T
Ivan Starkov 482839e83a chore: Remove prisma from the Builder app (#4182)
## Description

1. What is this PR about (link the issue and add a short description)

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-10-01 13:51:50 +03:00

93 lines
2.2 KiB
TypeScript

import type { Database } from "@webstudio-is/postrest/index.server";
import type { AppContext } from "@webstudio-is/trpc-interface/index.server";
import type { GitHubProfile } from "remix-auth-github";
import type { GoogleProfile } from "remix-auth-google";
export type User = Database["public"]["Tables"]["User"]["Row"];
export const getUserById = async (context: AppContext, id: User["id"]) => {
const dbUser = await context.postgrest.client
.from("User")
.select()
.eq("id", id)
.single();
if (dbUser.error) {
console.error(dbUser.error);
throw new Error("User not found");
}
return dbUser.data;
};
const genericCreateAccount = async (
context: AppContext,
userData: {
email: string;
username: string;
image: string;
provider: string;
}
): Promise<User> => {
const dbUser = await context.postgrest.client
.from("User")
.select()
.eq("email", userData.email)
.single();
if (dbUser.error == null) {
return dbUser.data;
}
// https://github.com/PostgREST/postgrest/blob/bfbd033c6e9f38cfbc8b1cfe19ee009a9379e3dd/docs/references/errors.rst#L234
if (dbUser.error.code !== "PGRST116") {
console.error(dbUser.error);
throw new Error("User not found");
}
const newUser = await context.postgrest.client
.from("User")
.insert({
id: crypto.randomUUID(),
...userData,
})
.select()
.single();
if (newUser.error) {
console.error(newUser.error);
throw new Error("Failed to create user");
}
return newUser.data;
};
export const createOrLoginWithOAuth = async (
context: AppContext,
profile: GoogleProfile | GitHubProfile
): Promise<User> => {
const userData = {
email: (profile.emails ?? [])[0]?.value,
username: profile.displayName,
image: (profile.photos ?? [])[0]?.value,
provider: profile.provider,
};
const newUser = await genericCreateAccount(context, userData);
return newUser;
};
export const createOrLoginWithDev = async (
context: AppContext,
email: string
): Promise<User> => {
const userData = {
email,
username: "admin",
image: "",
provider: "dev",
};
const newUser = await genericCreateAccount(context, userData);
return newUser;
};