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.
This commit is contained in:
Bogdan Chadkin authored and GitHub committed 2022-12-08 15:28:39 +03:00
1 parent b39e6a6376
commit 9e8bbffaa9
4 files changed
+19 -8

No files matched your search

+3 -3
View File
@@ -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({}));
}
+2 -2
View File
@@ -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());
}
@@ -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({
+12 -1
View File
@@ -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;
};