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.

add alerts in the case of errors

Sara Vieira8ae57d95079f
diff --git a/app/routes/auth/dev.tsx b/app/routes/auth/dev.tsx
index 32cbe47da1..2e20c451e1 100644
--- a/app/routes/auth/dev.tsx
+++ b/app/routes/auth/dev.tsx
@@ -1,14 +1,26 @@
-import { ActionFunction } from "@remix-run/node";
+import { ActionFunction, redirect } from "@remix-run/node";
import config from "~/config";
import { authenticator } from "~/services/auth.server";
+import { LOGIN_ERROR_CODES } from "~/shared/session/useLoginErrors";
export default function Dev() {
return null;
}
-export const action: ActionFunction = ({ request }) => {
- return authenticator.authenticate("dev", request, {
- successRedirect: config.dashboardPath,
- failureRedirect: config.loginPath,
- });
+export const action: ActionFunction = async ({ request }) => {
+ try {
+ return await authenticator.authenticate("dev", request, {
+ successRedirect: config.dashboardPath,
+ throwOnError: true,
+ });
+ } catch (error: any) {
+ // all redirects are basically errors and in that case we don't want to catch it
+ if (error instanceof Response) return error;
+ console.log(error);
+ return redirect(
+ `${config.loginPath}?error=${LOGIN_ERROR_CODES.LOGIN_DEV}&message=${
+ error?.message || ""
+ }`
+ );
+ }
};
diff --git a/app/routes/auth/github.tsx b/app/routes/auth/github.tsx
index eb1ef90951..f6537a7aca 100644
--- a/app/routes/auth/github.tsx
+++ b/app/routes/auth/github.tsx
@@ -1,19 +1,31 @@
-import { ActionFunction } from "@remix-run/node";
+import { ActionFunction, redirect } from "@remix-run/node";
import config from "~/config";
import { authenticator } from "~/services/auth.server";
import { ensureUserCookie } from "~/shared/session";
+import { LOGIN_ERROR_CODES } from "~/shared/session/useLoginErrors";
export default function GH() {
return null;
}
export const action: ActionFunction = async ({ request }) => {
- const { userId } = await ensureUserCookie(request);
- return authenticator.authenticate("github", request, {
- context: {
- userId,
- },
- successRedirect: config.dashboardPath,
- failureRedirect: config.loginPath,
- });
+ try {
+ const { userId } = await ensureUserCookie(request);
+ return await authenticator.authenticate("github", request, {
+ context: {
+ userId,
+ },
+ successRedirect: config.dashboardPath,
+ throwOnError: true,
+ });
+ } catch (error: any) {
+ // all redirects are basically errors and in that case we don't want to catch it
+ if (error instanceof Response) return error;
+ console.log(error);
+ return redirect(
+ `${config.loginPath}?error=${LOGIN_ERROR_CODES.LOGIN_GITHUB}&message=${
+ error?.message || ""
+ }`
+ );
+ }
};
diff --git a/app/routes/auth/google.tsx b/app/routes/auth/google.tsx
index 06c06a589d..985b6afd82 100644
--- a/app/routes/auth/google.tsx
+++ b/app/routes/auth/google.tsx
@@ -2,6 +2,7 @@ import { ActionFunction, LoaderFunction, redirect } from "@remix-run/node";
import config from "~/config";
import { authenticator } from "~/services/auth.server";
import { ensureUserCookie } from "~/shared/session";
+import { LOGIN_ERROR_CODES } from "~/shared/session/useLoginErrors";
export default function Google() {
return null;
@@ -10,12 +11,23 @@ export default function Google() {
export const loader: LoaderFunction = () => redirect("/login");
export const action: ActionFunction = async ({ request }) => {
- const { userId } = await ensureUserCookie(request);
- return authenticator.authenticate("google", request, {
- context: {
- userId,
- },
- successRedirect: config.dashboardPath,
- failureRedirect: config.loginPath,
- });
+ try {
+ const { userId } = await ensureUserCookie(request);
+ return await authenticator.authenticate("google", request, {
+ context: {
+ userId,
+ },
+ successRedirect: config.dashboardPath,
+ throwOnError: true,
+ });
+ } catch (error: any) {
+ // all redirects are basically errors and in that case we don't want to catch it
+ if (error instanceof Response) return error;
+ console.log(error);
+ return redirect(
+ `${config.loginPath}?error=${LOGIN_ERROR_CODES.LOGIN_GOOGLE}&message=${
+ error?.message || ""
+ }`
+ );
+ }
};
diff --git a/app/routes/login/index.tsx b/app/routes/login/index.tsx
index 091979a839..697ccd044d 100644
--- a/app/routes/login/index.tsx
+++ b/app/routes/login/index.tsx
@@ -4,6 +4,7 @@ import { authenticator } from "~/services/auth.server";
import config from "~/config";
import { Login, links } from "~/auth/";
+import { useLoginErrors } from "~/shared/session/useLoginErrors";
export { links };
@@ -17,6 +18,7 @@ export const loader: LoaderFunction = async ({ request }) => {
};
const LoginRoute = () => {
+ useLoginErrors();
return <Login />;
};
diff --git a/app/services/auth.server.ts b/app/services/auth.server.ts
index eaa9389177..d7f7aaa6cc 100644
--- a/app/services/auth.server.ts
+++ b/app/services/auth.server.ts
@@ -48,8 +48,8 @@ const google = new GoogleStrategy(
export const authenticator = new Authenticator<User>(sessionStorage);
if (process.env.GH_CLIENT_ID && process.env.GH_CLIENT_SECRET) {
authenticator.use(github, "github");
- authenticator.use(google, "google");
}
+authenticator.use(google, "google");
if (process.env.DEV_LOGIN === "true") {
authenticator.use(
@@ -60,8 +60,8 @@ if (process.env.DEV_LOGIN === "true") {
try {
const user = await createOrLoginWithDev(secret);
return user;
- } catch (e) {
- console.log(e);
+ } catch (error: any) {
+ throw new Error(error);
}
}
diff --git a/app/shared/session/useLoginErrors.ts b/app/shared/session/useLoginErrors.ts
new file mode 100644
index 0000000000..93b9d0f227
--- /dev/null
+++ b/app/shared/session/useLoginErrors.ts
@@ -0,0 +1,49 @@
+import { useEffect } from "react";
+import { useSearchParams } from "react-router-dom";
+
+export const LOGIN_ERROR_CODES = {
+ LOGIN_DEV: "login_dev",
+ LOGIN_GITHUB: "login_github",
+ LOGIN_GOOGLE: "login_google",
+};
+
+export const LOGIN_ERROR_MESSAGES = {
+ [LOGIN_ERROR_CODES.LOGIN_DEV]:
+ "There has been an issue logging you in with dev",
+ [LOGIN_ERROR_CODES.LOGIN_GITHUB]:
+ "There has been an issue logging you in with Github",
+ [LOGIN_ERROR_CODES.LOGIN_GOOGLE]:
+ "There has been an issue logging you in with Google",
+};
+
+export const useLoginErrors = () => {
+ const [searchParams, setSearchParams] = useSearchParams();
+ const error = searchParams.get("error");
+ const message = searchParams.get("message");
+ useEffect(() => {
+ if (error !== null && !!message) {
+ console.log(message);
+ alert(message);
+
+ setSearchParams({});
+ return;
+ }
+
+ switch (error) {
+ case LOGIN_ERROR_CODES.LOGIN_DEV:
+ alert(LOGIN_ERROR_MESSAGES[LOGIN_ERROR_CODES.LOGIN_DEV]);
+ break;
+ case LOGIN_ERROR_CODES.LOGIN_GITHUB:
+ alert(LOGIN_ERROR_MESSAGES[LOGIN_ERROR_CODES.LOGIN_GITHUB]);
+ break;
+ case LOGIN_ERROR_CODES.LOGIN_GOOGLE:
+ alert(LOGIN_ERROR_MESSAGES[LOGIN_ERROR_CODES.LOGIN_GOOGLE]);
+ break;
+
+ default:
+ break;
+ }
+
+ setSearchParams({});
+ }, [error, message, searchParams, setSearchParams]);
+};
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 926f331193..c7a5edb888 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -10,6 +10,12 @@ datasource db {
url = env("DATABASE_URL")
}
+model Team {
+ id String @id @default(uuid())
+ users User[]
+
+}
+
model User {
id String @id @default(uuid())
email String? @unique
@@ -17,18 +23,20 @@ model User {
image String?
username String?
createdAt DateTime @default(now())
+ Team Team? @relation(fields: [teamId], references: [id])
+ teamId String?
projects Project[]
}
model Project {
id String @id @default(uuid())
- userId String
title String
domain String @unique
prodTreeId String? // exists when published
devTreeId String
prodTreeIdHistory String @default("[]")
User User? @relation(fields: [userId], references: [id])
+ userId String?
}
model Tree {
diff --git a/yarn.lock b/yarn.lock
index ada3e74e10..e572c9589a 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4374,10 +4374,10 @@
"@webassemblyjs/wast-parser" "1.9.0"
"@xtuc/long" "4.2.2"
-"@webstudio-is/[email protected]":
- version "0.3.2"
- resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.3.2.tgz#abb9d9be162511232202125d03bb4b23e551b608"
- integrity sha512-+q+PYzCKWq7Me/JQNjBveUHY4vm1rr364pW5FCA1HN00heEQolQJwgg3ION9MKjYfSSaUjny7dPRtb/dV5ETGw==
+"@webstudio-is/[email protected]":
+ version "0.4.0"
+ resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.4.0.tgz#dd8f17e2663f6373fcde5c11999f1b3566c40a75"
+ integrity sha512-Im6vc5v6SIpd8663Y0BWIJLoIm9zxGj4WVo3cDaC5tNJbPuBRMMm5eLIYaFmNApfGimftQqNDasELOViQsEMag==
dependencies:
"@prisma/client" "^3.10.0"
"@stitches/core" "^1.2.7"