Skip to content

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.

Check authenticated user against db (#542)

Bogdan Chadkin9e8bbffaa976
diff --git a/apps/designer/app/routes/dashboard/index.tsx b/apps/designer/app/routes/dashboard/index.tsx
index 64e5dbd419..382f842862 100644
--- a/apps/designer/app/routes/dashboard/index.tsx
+++ b/apps/designer/app/routes/dashboard/index.tsx
@@ -8,7 +8,7 @@ import {
import { Dashboard, links } from "~/dashboard";
import { db } from "@webstudio-is/project/server";
import { ensureUserCookie } from "~/shared/session";
-import { authenticator } from "~/services/auth.server";
+import { findAuthenticatedUser } from "~/services/auth.server";
import { zfd } from "zod-form-data";
import { type Project, User as DbUser } from "@webstudio-is/prisma-client";
import { designerPath, loginPath } from "~/shared/router-utils";
@@ -31,7 +31,7 @@ export const action: ActionFunction = async ({ request }) => {
const { project: title } = schema.parse(await request.formData());
const { userId, headers } = await ensureUserCookie(request);
- const authenticatedUser = await authenticator.isAuthenticated(request);
+ const authenticatedUser = await findAuthenticatedUser(request);
try {
const project = await db.project.create({
title,
@@ -47,7 +47,7 @@ export const action: ActionFunction = async ({ request }) => {
};
export const loader: LoaderFunction = async ({ request }) => {
- const user = await authenticator.isAuthenticated(request);
+ const user = await findAuthenticatedUser(request);
if (!user) {
return redirect(loginPath({}));
}
diff --git a/apps/designer/app/routes/login/index.tsx b/apps/designer/app/routes/login/index.tsx
index b83b5361eb..e48215965e 100644
--- a/apps/designer/app/routes/login/index.tsx
+++ b/apps/designer/app/routes/login/index.tsx
@@ -1,6 +1,6 @@
import { LoaderFunction, redirect } from "@remix-run/node";
-import { authenticator } from "~/services/auth.server";
+import { findAuthenticatedUser } from "~/services/auth.server";
import env from "~/env.server";
import { Login, links } from "~/auth";
@@ -10,7 +10,7 @@ import { dashboardPath } from "~/shared/router-utils";
export { links };
export const loader: LoaderFunction = async ({ request }) => {
- const user = await authenticator.isAuthenticated(request);
+ const user = await findAuthenticatedUser(request);
if (user) {
return redirect(dashboardPath());
}
diff --git a/apps/designer/app/routes/rest/project/clone.$domain.ts b/apps/designer/app/routes/rest/project/clone.$domain.ts
index 1a55ef20e7..b556233731 100644
--- a/apps/designer/app/routes/rest/project/clone.$domain.ts
+++ b/apps/designer/app/routes/rest/project/clone.$domain.ts
@@ -4,7 +4,7 @@ import { db as projectDb } from "@webstudio-is/project/server";
import { type Project } from "@webstudio-is/project";
import * as userDb from "~/shared/db";
import { ensureUserCookie } from "~/shared/session";
-import { authenticator } from "~/services/auth.server";
+import { findAuthenticatedUser } from "~/services/auth.server";
import { designerPath } from "~/shared/router-utils";
const ensureProject = async ({
@@ -30,7 +30,7 @@ const ensureProject = async ({
*/
export const loader: LoaderFunction = async ({ request, params }) => {
if (params.domain === undefined) return { errors: "Domain required" };
- const user = await authenticator.isAuthenticated(request);
+ const user = await findAuthenticatedUser(request);
const { headers, userId: generatedUserId } = await ensureUserCookie(request);
try {
const userId = await userDb.user.ensureUser({
diff --git a/apps/designer/app/services/auth.server.ts b/apps/designer/app/services/auth.server.ts
index 287d949330..684ecfd8bc 100644
--- a/apps/designer/app/services/auth.server.ts
+++ b/apps/designer/app/services/auth.server.ts
@@ -1,4 +1,4 @@
-import { User } from "@webstudio-is/prisma-client";
+import { type User, prisma } from "@webstudio-is/prisma-client";
import { Authenticator } from "remix-auth";
import { FormStrategy } from "remix-auth-form";
import { GitHubStrategy, type GitHubProfile } from "remix-auth-github";
@@ -92,3 +92,14 @@ if (process.env.DEV_LOGIN === "true") {
"dev"
);
}
+
+export const findAuthenticatedUser = async (request: Request) => {
+ const user = await authenticator.isAuthenticated(request);
+ if (user != null) {
+ const dbUser = await prisma.user.findUnique({
+ where: { id: user.id },
+ });
+ return dbUser;
+ }
+ return null;
+};