Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
migrate projects
Sara Vieira4b295e7a5a10
diff --git a/app/routes/auth/github.tsx b/app/routes/auth/github.tsxindex 5b5cfd855c..9841230333 100644--- a/app/routes/auth/github.tsx+++ b/app/routes/auth/github.tsx@@ -1,9 +1,14 @@import { ActionFunction } from "@remix-run/node";import config from "~/config";import { authenticator } from "~/services/auth.server";+import { ensureUserCookie } from "~/shared/session";-export const action: ActionFunction = ({ request }) => {+export const action: ActionFunction = async ({ request }) => {+ const userId = await ensureUserCookie(request);return authenticator.authenticate("github", request, {+ context: {+ ...userId,+ },successRedirect: config.dashboardPath,failureRedirect: config.loginPath,});diff --git a/app/routes/auth/github/callback.tsx b/app/routes/auth/github/callback.tsxindex 3b821aaeb3..7c9e8feff4 100644--- a/app/routes/auth/github/callback.tsx+++ b/app/routes/auth/github/callback.tsx@@ -1,9 +1,14 @@import { LoaderFunction } from "@remix-run/node";import config from "~/config";import { authenticator } from "~/services/auth.server";+import { ensureUserCookie } from "~/shared/session";-export const loader: LoaderFunction = ({ request }) => {+export const loader: LoaderFunction = async ({ request }) => {+ const userId = await ensureUserCookie(request);return authenticator.authenticate("github", request, {+ context: {+ ...userId,+ },successRedirect: config.dashboardPath,failureRedirect: config.loginPath,});diff --git a/app/services/auth.server.ts b/app/services/auth.server.tsindex d7cf054928..5b3f6015e3 100644--- a/app/services/auth.server.ts+++ b/app/services/auth.server.ts@@ -20,8 +20,8 @@ const github = new GitHubStrategy(clientSecret: process.env.GH_CLIENT_SECRET as string,callbackURL: `${url}/github/callback`,},- async ({ profile }) => {- const user = createOrLoginWithGithub(profile);+ async ({ profile, context }) => {+ const user = createOrLoginWithGithub(profile, context?.userId);return user;}diff --git a/app/shared/db/project.server.ts b/app/shared/db/project.server.tsindex 8e1d60c3fb..c298384001 100644--- a/app/shared/db/project.server.ts+++ b/app/shared/db/project.server.ts@@ -75,7 +75,16 @@ export const create = async ({const tree = await db.tree.create(db.tree.createRootInstance(breakpoints));const project = await prisma.project.create({data: {- userId,+ User: {+ connectOrCreate: {+ create: {+ id: userId as string,+ },+ where: {+ id: userId as string,+ },+ },+ },title,domain,devTreeId: tree.id,@@ -96,11 +105,20 @@ export const clone = async (clonableDomain: string): Promise<Project> => {const tree = await db.tree.clone(clonableProject.prodTreeId);const domain = generateDomain(clonableProject.title);-+ console.log(clonableProject.userId);const [project] = await Promise.all([prisma.project.create({data: {- userId: clonableProject.userId,+ User: {+ connectOrCreate: {+ create: {+ id: clonableProject.userId as string,+ },+ where: {+ id: clonableProject.userId as string,+ },+ },+ },title: clonableProject.title,domain,devTreeId: tree.id,diff --git a/app/shared/db/user.server.ts b/app/shared/db/user.server.tsindex 1965064bd7..a059a7cec5 100644--- a/app/shared/db/user.server.ts+++ b/app/shared/db/user.server.ts@@ -1,12 +1,30 @@import { User } from "@prisma/client";import { GitHubProfile } from "remix-auth-github";import { GoogleProfile } from "remix-auth-google";-import { ensureUserCookie } from "../session";import { prisma } from "./prisma.server";export const createOrLoginWithGithub = async (- profile: GitHubProfile+ profile: GitHubProfile,+ userId: string | null): Promise<User> => {+ const userData = {+ email: profile._json.email,+ username: profile.displayName,+ image: profile._json.avatar_url,+ provider: profile.provider,+ };+ if (userId) {+ const connectedUser = await prisma.user.update({+ where: {+ id: userId,+ },+ data: {+ ...userData,+ },+ });++ return connectedUser;+ }const user = await prisma.user.findUnique({where: { email: profile._json.email },});@@ -42,7 +60,7 @@ export const createOrLoginWithGoogle = async (data: {email: profile._json.email,username: profile.displayName,- image: profile._json.avatar_url,+ image: profile._json.picture,provider: profile.provider,},});diff --git a/package.json b/package.jsonindex 099c9837e7..dd4b4ae77c 100644--- a/package.json+++ b/package.json@@ -4,7 +4,7 @@"description": "Webstudio Designer UI as a service","scripts": {"build": "remix build",- "dev": "yarn setup && node -r dotenv/config node_modules/.bin/remix dev",+ "dev": "yarn setup && HOST=localhost node -r dotenv/config node_modules/.bin/remix dev","start": "remix-serve build","typecheck": "tsc -b","lint": "eslint . --ext .ts,.tsx --max-warnings 0",diff --git a/prisma/migrations/20220616143541_add_projects/migration.sql b/prisma/migrations/20220616143541_add_projects/migration.sqlnew file mode 100644index 0000000000..6acbed2683--- /dev/null+++ b/prisma/migrations/20220616143541_add_projects/migration.sql@@ -0,0 +1,2 @@+-- AddForeignKey+ALTER TABLE "Project" ADD CONSTRAINT "Project_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;diff --git a/prisma/migrations/20220616143902_userid_not_mandatory/migration.sql b/prisma/migrations/20220616143902_userid_not_mandatory/migration.sqlnew file mode 100644index 0000000000..bbc5961246--- /dev/null+++ b/prisma/migrations/20220616143902_userid_not_mandatory/migration.sql@@ -0,0 +1,8 @@+-- DropForeignKey+ALTER TABLE "Project" DROP CONSTRAINT "Project_userId_fkey";++-- AlterTable+ALTER TABLE "Project" ALTER COLUMN "userId" DROP NOT NULL;++-- AddForeignKey+ALTER TABLE "Project" ADD CONSTRAINT "Project_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 1eda4ff425..32ee35bb03 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -22,13 +22,13 @@ model User {model Project {id String @id @default(uuid())- userId String+ userId String?title Stringdomain String @uniqueprodTreeId String? // exists when publisheddevTreeId StringprodTreeIdHistory String @default("[]")- User User @relation(fields: [userId], references: [id])+ User User? @relation(fields: [userId], references: [id])}model Tree {