From 9e8bbffaa97696b98ec8a4635c52be7bcae09c3c Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 8 Dec 2022 15:28:39 +0300 Subject: [PATCH] Check authenticated user against db (#542) Ref https://github.com/webstudio-is/webstudio-designer/issues/92 We store user session in storage and checked its existance to check if user is logged in. Though where we clear db or for example delete user this session becomes stale so we should check if user exists in db too. --- apps/designer/app/routes/dashboard/index.tsx | 6 +++--- apps/designer/app/routes/login/index.tsx | 4 ++-- .../app/routes/rest/project/clone.$domain.ts | 4 ++-- apps/designer/app/services/auth.server.ts | 13 ++++++++++++- 4 files changed, 19 insertions(+), 8 deletions(-) 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; +};