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.

start work on auth

Sara Vieira0cc6bcece381
diff --git a/.env.example b/.env.example
index e67ca13c54..f170dc77ec 100644
--- a/.env.example
+++ b/.env.example
@@ -1 +1,4 @@
-DATABASE_URL=postgresql://user:pass@localhost/webstudio
\ No newline at end of file
+DATABASE_URL=postgresql://user:pass@localhost/webstudio
+AUTH_SECRET="# Linux: `openssl rand -hex 32` or go to https://generate-secret.now.sh/32"
+GH_CLIENT_SECRET=
+GH_CLIENT_ID=
\ No newline at end of file
diff --git a/app/routes/auth/github.tsx b/app/routes/auth/github.tsx
new file mode 100644
index 0000000000..f0b627356c
--- /dev/null
+++ b/app/routes/auth/github.tsx
@@ -0,0 +1,8 @@
+import { ActionFunction, LoaderFunction, redirect } from "@remix-run/node";
+import { authenticator } from "~/services/auth.server";
+
+export const loader: LoaderFunction = () => redirect("/login");
+
+export const action: ActionFunction = ({ request }) => {
+ return authenticator.authenticate("github", request);
+};
diff --git a/app/routes/auth/github/callback.tsx b/app/routes/auth/github/callback.tsx
new file mode 100644
index 0000000000..5b77ad6add
--- /dev/null
+++ b/app/routes/auth/github/callback.tsx
@@ -0,0 +1,9 @@
+import { LoaderFunction } from "@remix-run/node";
+import { authenticator } from "~/services/auth.server";
+
+export const loader: LoaderFunction = ({ request }) => {
+ return authenticator.authenticate("github", request, {
+ successRedirect: "/dashboard",
+ failureRedirect: "/login",
+ });
+};
diff --git a/app/routes/dashboard/index.tsx b/app/routes/dashboard/index.tsx
index a87d6986a7..886434ee40 100644
--- a/app/routes/dashboard/index.tsx
+++ b/app/routes/dashboard/index.tsx
@@ -9,6 +9,7 @@ import { Dashboard, links } from "~/dashboard";
import * as db from "~/shared/db";
import config from "~/config";
import { ensureUserCookie } from "~/shared/session";
+import { authenticator } from "~/services/auth.server";
export { links };
@@ -29,6 +30,11 @@ export const action: ActionFunction = async ({ request }) => {
};
export const loader: LoaderFunction = async ({ request }) => {
+ const user = await authenticator.isAuthenticated(request);
+ console.log(user);
+ if (!user) {
+ return redirect("/login");
+ }
const { userId, headers } = await ensureUserCookie(request);
const projects = await db.project.loadManyByUserId(userId);
return json({ config, projects }, headers);
diff --git a/app/routes/login.tsx b/app/routes/login.tsx
new file mode 100644
index 0000000000..f1e543c7b4
--- /dev/null
+++ b/app/routes/login.tsx
@@ -0,0 +1,9 @@
+import { Form } from "@remix-run/react";
+
+export default function Login() {
+ return (
+ <Form action="/auth/github" method="post">
+ <button>Login with GitHub</button>
+ </Form>
+ );
+}
diff --git a/app/routes/logout.tsx b/app/routes/logout.tsx
new file mode 100644
index 0000000000..6799dd897a
--- /dev/null
+++ b/app/routes/logout.tsx
@@ -0,0 +1,10 @@
+import { ActionFunction } from "@remix-run/node";
+import { authenticator } from "~/services/auth.server";
+
+export default function Login() {
+ return "loging you out";
+}
+
+export const loader: ActionFunction = async ({ request }) => {
+ await authenticator.logout(request, { redirectTo: "/login" });
+};
diff --git a/app/services/auth.server.ts b/app/services/auth.server.ts
new file mode 100644
index 0000000000..4c4ebb7214
--- /dev/null
+++ b/app/services/auth.server.ts
@@ -0,0 +1,28 @@
+import { User } from "@prisma/client";
+import { Authenticator } from "remix-auth";
+import { GitHubStrategy } from "remix-auth-github";
+import { sessionStorage } from "~/services/session.server";
+import { createOrLoginWithGithub } from "~/shared/db/user.server";
+
+// Create an instance of the authenticator, pass a generic with what
+// strategies will return and will store in the session
+export const authenticator = new Authenticator<User>(sessionStorage);
+if (process.env.GH_CLIENT_ID && process.env.GH_CLIENT_SECRET) {
+ authenticator.use(
+ new GitHubStrategy(
+ {
+ clientID: process.env.GH_CLIENT_ID as string,
+ clientSecret: process.env.GH_CLIENT_SECRET as string,
+ callbackURL: `https://${
+ process.env.VERCEL_URL || "192.168.1.91:3000"
+ }/auth/github/callback`,
+ },
+ async ({ profile }) => {
+ const user = createOrLoginWithGithub(profile);
+
+ return user;
+ }
+ ),
+ "github"
+ );
+}
diff --git a/app/services/session.server.ts b/app/services/session.server.ts
new file mode 100644
index 0000000000..423d3c303b
--- /dev/null
+++ b/app/services/session.server.ts
@@ -0,0 +1,16 @@
+import { createCookieSessionStorage } from "@remix-run/node";
+
+// export the whole sessionStorage object
+export const sessionStorage = createCookieSessionStorage({
+ cookie: {
+ name: "_session", // use any name you want here
+ sameSite: "lax", // this helps with CSRF
+ path: "/", // remember to add this so the cookie will work in all routes
+ httpOnly: true, // for security reasons, make this cookie http only
+ secrets: [process.env.AUTH_SECRET as string], // replace this with an actual secret
+ secure: process.env.NODE_ENV === "production", // enable this in prod only
+ },
+});
+
+// you can also export the methods individually for your own usage
+export const { getSession, commitSession, destroySession } = sessionStorage;
diff --git a/app/shared/db/user.server.ts b/app/shared/db/user.server.ts
new file mode 100644
index 0000000000..eddd8633a0
--- /dev/null
+++ b/app/shared/db/user.server.ts
@@ -0,0 +1,26 @@
+import { User } from "@prisma/client";
+import { GitHubProfile } from "remix-auth-github";
+import { prisma } from "./prisma.server";
+
+export const createOrLoginWithGithub = async (
+ profile: GitHubProfile
+): Promise<User> => {
+ const user = await prisma.user.findUnique({
+ where: { email: profile._json.email },
+ });
+
+ if (user) {
+ return user;
+ }
+
+ const newUser = prisma.user.create({
+ data: {
+ email: profile._json.email,
+ username: profile.displayName,
+ image: profile._json.avatar_url,
+ provider: profile.provider,
+ },
+ });
+
+ return newUser;
+};
diff --git a/package.json b/package.json
index 58ad75b805..b818f8c8eb 100644
--- a/package.json
+++ b/package.json
@@ -70,6 +70,8 @@
"react-nano-state": "^0.4.0",
"react-router-dom": "^6.2.2",
"react-use": "^17.3.2",
+ "remix-auth": "^3.2.2",
+ "remix-auth-github": "^1.1.1",
"slugify": "^1.6.5"
},
"devDependencies": {
diff --git a/prisma/migrations/20220608130924_/migration.sql b/prisma/migrations/20220608130924_/migration.sql
new file mode 100644
index 0000000000..2640ef8e8e
--- /dev/null
+++ b/prisma/migrations/20220608130924_/migration.sql
@@ -0,0 +1,15 @@
+/*
+ Warnings:
+
+ - A unique constraint covering the columns `[email]` on the table `User` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "User" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ADD COLUMN "email" TEXT,
+ADD COLUMN "image" TEXT,
+ADD COLUMN "provider" TEXT,
+ADD COLUMN "username" TEXT;
+
+-- CreateIndex
+CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
diff --git a/prisma/migrations/20220608130959_adduser/migration.sql b/prisma/migrations/20220608130959_adduser/migration.sql
new file mode 100644
index 0000000000..9449a3e7dc
--- /dev/null
+++ b/prisma/migrations/20220608130959_adduser/migration.sql
@@ -0,0 +1,19 @@
+/*
+ Warnings:
+
+ - You are about to drop the column `createdAt` on the `User` table. All the data in the column will be lost.
+ - You are about to drop the column `email` on the `User` table. All the data in the column will be lost.
+ - You are about to drop the column `image` on the `User` table. All the data in the column will be lost.
+ - You are about to drop the column `provider` on the `User` table. All the data in the column will be lost.
+ - You are about to drop the column `username` on the `User` table. All the data in the column will be lost.
+
+*/
+-- DropIndex
+DROP INDEX "User_email_key";
+
+-- AlterTable
+ALTER TABLE "User" DROP COLUMN "createdAt",
+DROP COLUMN "email",
+DROP COLUMN "image",
+DROP COLUMN "provider",
+DROP COLUMN "username";
diff --git a/prisma/migrations/20220608131719_add_user/migration.sql b/prisma/migrations/20220608131719_add_user/migration.sql
new file mode 100644
index 0000000000..2640ef8e8e
--- /dev/null
+++ b/prisma/migrations/20220608131719_add_user/migration.sql
@@ -0,0 +1,15 @@
+/*
+ Warnings:
+
+ - A unique constraint covering the columns `[email]` on the table `User` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "User" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ADD COLUMN "email" TEXT,
+ADD COLUMN "image" TEXT,
+ADD COLUMN "provider" TEXT,
+ADD COLUMN "username" TEXT;
+
+-- CreateIndex
+CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index f2c4830093..17d8d0520d 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -10,6 +10,15 @@ datasource db {
url = env("DATABASE_URL")
}
+model User {
+ id String @id @default(uuid())
+ email String? @unique
+ provider String?
+ image String?
+ username String?
+ createdAt DateTime @default(now())
+}
+
model Project {
id String @id @default(uuid())
userId String
@@ -20,10 +29,6 @@ model Project {
prodTreeIdHistory String @default("[]")
}
-model User {
- id String @id @default(uuid())
-}
-
model Tree {
id String @id @default(uuid())
root String
diff --git a/yarn.lock b/yarn.lock
index e940128508..5b6226dfd5 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1875,10 +1875,10 @@
resolved "https://registry.yarnpkg.com/@web3-storage/multipart-parser/-/multipart-parser-1.0.0.tgz#6b69dc2a32a5b207ba43e556c25cc136a56659c4"
integrity sha512-BEO6al7BYqcnfX15W2cnGR+Q566ACXAT9UQykORCWW80lmkpWsnEob6zJS1ZVBKsSJC8+7vJkHwlp+lXG1UCdw==
-"@webstudio-is/[email protected]":
- version "0.2.3-postgres"
- resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.2.3-postgres.tgz#f0c820b58e2fb722787a42546d8dd3e82adfd7f4"
- integrity sha512-mTV2+m6nmy3m3mzaz/MSlStVL9E93al4BDoaSpOePssr8xXJXB4Jw/hUHEbrfKB7h2NqU3e9pDQ/aLkoH0n13g==
+"@webstudio-is/[email protected]":
+ version "0.3.1"
+ resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.3.1.tgz#6026d384284ff17093bd338531017609fc3f530c"
+ integrity sha512-6hnFRoXjWQqxA1wRdyJ9S+BHdlbpSZZi6USNvRrwrXebmzm/axonJyEwOJVQbj9H5DAcYRlpe9jFJOKWsuNhmw==
dependencies:
"@prisma/client" "^3.10.0"
"@stitches/core" "^1.2.7"
@@ -2844,6 +2844,13 @@ debug@4, debug@^4.0.0, debug@^4.1.0, debug@^4.1.1, debug@^4.3.2:
dependencies:
ms "2.1.2"
+debug@^4.3.4:
+ version "4.3.4"
+ resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
+ integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
+ dependencies:
+ ms "2.1.2"
+
decamelize-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/decamelize-keys/-/decamelize-keys-1.1.0.tgz#d171a87933252807eb3cb61dc1c1445d078df2d9"
@@ -6940,6 +6947,14 @@ react-dom@^17.0.2:
object-assign "^4.1.1"
scheduler "^0.20.2"
+react-dom@^18.1.0:
+ version "18.1.0"
+ resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.1.0.tgz#7f6dd84b706408adde05e1df575b3a024d7e8a2f"
+ integrity sha512-fU1Txz7Budmvamp7bshe4Zi32d0ll7ect+ccxNu9FlObT605GOEB8BfO4tmRJ39R5Zj831VCpvQ05QPBW5yb+w==
+ dependencies:
+ loose-envify "^1.1.0"
+ scheduler "^0.22.0"
+
react-hotkeys-hook@^3.4.4:
version "3.4.4"
resolved "https://registry.yarnpkg.com/react-hotkeys-hook/-/react-hotkeys-hook-3.4.4.tgz#52ba5d8ef5e47cc2e776c70a9036d518e0993d51"
@@ -7182,6 +7197,31 @@ remark-rehype@^9.0.0:
mdast-util-to-hast "^11.0.0"
unified "^10.0.0"
+remix-auth-github@^1.1.1:
+ version "1.1.1"
+ resolved "https://registry.yarnpkg.com/remix-auth-github/-/remix-auth-github-1.1.1.tgz#8b5a58bd28678becaf85d2187b74b5b7ad805aa4"
+ integrity sha512-/LHX0u8AsJUV02T2WC+f6NRWqsYwsWJY+1UUSW2E+lXyo6mf6ajlM802Iem/F74OxNVukOYJerwwoludwGJj6Q==
+ dependencies:
+ remix-auth "^3.0.0"
+ remix-auth-oauth2 "^1.0.0"
+
+remix-auth-oauth2@^1.0.0:
+ version "1.2.2"
+ resolved "https://registry.yarnpkg.com/remix-auth-oauth2/-/remix-auth-oauth2-1.2.2.tgz#bebe0ac586744ed94885bd47f6bcb710d377fede"
+ integrity sha512-bbQw/SFAbilSE1zrZFaJN/NM7WUkC63CKJFVz2e/Y21qqMZo5eVQXooSV6d23mABfodeVk6wthngdLAjxak6fQ==
+ dependencies:
+ debug "^4.3.4"
+ react-dom "^18.1.0"
+ remix-auth "^3.2.2"
+ uuid "^8.3.2"
+
+remix-auth@^3.0.0, remix-auth@^3.2.2:
+ version "3.2.2"
+ resolved "https://registry.yarnpkg.com/remix-auth/-/remix-auth-3.2.2.tgz#8941a6b57cd523872b7aacf200976d77c055a9f7"
+ integrity sha512-VtzkfxeXbnXilupRTZkP40aik4vFSdwwRT96mbq0UBDMqHVRfQ7h9Y51HFrTufHJZEfAdkCopedMVvm0vQYKag==
+ dependencies:
+ uuid "^8.3.2"
+
remove-trailing-separator@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"
@@ -7393,6 +7433,13 @@ scheduler@^0.20.2:
loose-envify "^1.1.0"
object-assign "^4.1.1"
+scheduler@^0.22.0:
+ version "0.22.0"
+ resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.22.0.tgz#83a5d63594edf074add9a7198b1bae76c3db01b8"
+ integrity sha512-6QAm1BgQI88NPYymgGQLCZgvep4FyePDWFpXVK+zNSUgHwlqpJy8VEh8Et0KxTACS4VWwMousBElAZOH9nkkoQ==
+ dependencies:
+ loose-envify "^1.1.0"
+
screenfull@^5.1.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/screenfull/-/screenfull-5.2.0.tgz#6533d524d30621fc1283b9692146f3f13a93d1ba"
@@ -8278,6 +8325,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
+uuid@^8.3.2:
+ version "8.3.2"
+ resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
+ integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
+
uvu@^0.5.0:
version "0.5.3"
resolved "https://registry.yarnpkg.com/uvu/-/uvu-0.5.3.tgz#3d83c5bc1230f153451877bfc7f4aea2392219ae"