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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix github/google login, don"t use _json key, github may not provide it
Oleg Isonenfec1fd1cac50
diff --git a/apps/designer/app/services/auth.server.ts b/apps/designer/app/services/auth.server.tsindex d1e9bb4f72..b14c66e110 100644--- a/apps/designer/app/services/auth.server.ts+++ b/apps/designer/app/services/auth.server.ts@@ -1,8 +1,8 @@import { User } from "@webstudio-is/prisma-client";import { Authenticator } from "remix-auth";import { FormStrategy } from "remix-auth-form";-import { GitHubStrategy } from "remix-auth-github";-import { GoogleStrategy } from "remix-auth-google";+import { GitHubStrategy, type GitHubProfile } from "remix-auth-github";+import { GoogleStrategy, type GoogleProfile } from "remix-auth-google";import config from "~/config";import * as db from "~/shared/db";import { sessionStorage } from "~/services/session.server";@@ -15,41 +15,57 @@ const url = `${: `http://localhost:${process.env.PORT || 3000}`}${config.authPath}`;-const github = new GitHubStrategy(- {- clientID: process.env.GH_CLIENT_ID as string,- clientSecret: process.env.GH_CLIENT_SECRET as string,- callbackURL: `${url}${config.githubCallbackPath}`,- },- async ({ profile }) => {- const user = await db.user.createOrLoginWithGithub(profile);-- return user;- }-);--const google = new GoogleStrategy(- {- clientID: process.env.GOOGLE_CLIENT_ID as string,- clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,- callbackURL: `${url}${config.googleCallbackPath}`,- },- async ({ profile }) => {- const user = await db.user.createOrLoginWithGoogle(profile);-+const strategyCallback = async ({+ profile,+}: {+ profile: GitHubProfile | GoogleProfile;+}) => {+ try {+ const user = await db.user.createOrLoginWithOAuth(profile);return user;+ } catch (error: unknown) {+ if (error instanceof Error) {+ sentryException({+ error,+ extras: {+ loginMethod: AUTH_PROVIDERS.LOGIN_DEV,+ },+ });+ }+ throw error;}-);+};// 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);+export const authenticator = new Authenticator<User>(sessionStorage, {+ throwOnError: true,+});+if (process.env.GH_CLIENT_ID && process.env.GH_CLIENT_SECRET) {+ const github = new GitHubStrategy(+ {+ clientID: process.env.GH_CLIENT_ID as string,+ clientSecret: process.env.GH_CLIENT_SECRET as string,+ callbackURL: `${url}${config.githubCallbackPath}`,+ },+ strategyCallback+ );authenticator.use(github, "github");}+if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {+ const google = new GoogleStrategy(+ {+ clientID: process.env.GOOGLE_CLIENT_ID as string,+ clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,+ callbackURL: `${url}${config.googleCallbackPath}`,+ },+ strategyCallback+ );authenticator.use(google, "google");}+if (process.env.DEV_LOGIN === "true") {authenticator.use(new FormStrategy(async ({ form }) => {@@ -68,6 +84,7 @@ if (process.env.DEV_LOGIN === "true") {},});}+ throw error;}}diff --git a/apps/designer/app/shared/db/user.server.ts b/apps/designer/app/shared/db/user.server.tsindex e12dbb1e5e..ca944f2d17 100644--- a/apps/designer/app/shared/db/user.server.ts+++ b/apps/designer/app/shared/db/user.server.ts@@ -1,5 +1,5 @@-import { GitHubProfile } from "remix-auth-github";-import { GoogleProfile } from "remix-auth-google";+import type { GitHubProfile } from "remix-auth-github";+import type { GoogleProfile } from "remix-auth-google";import { prisma, User } from "@webstudio-is/prisma-client";export const createDemoUser = async (userId: string) => {@@ -73,26 +73,13 @@ const genericCreateAccount = async (userData: {return newTeam.users[0];};-export const createOrLoginWithGithub = async (- profile: GitHubProfile+export const createOrLoginWithOAuth = async (+ profile: GoogleProfile | GitHubProfile): Promise<User> => {const userData = {- email: profile._json.email,+ email: (profile.emails ?? [])[0]?.value,username: profile.displayName,- image: profile._json.avatar_url,- provider: profile.provider,- };- const newUser = await genericCreateAccount(userData);- return newUser;-};--export const createOrLoginWithGoogle = async (- profile: GoogleProfile-): Promise<User> => {- const userData = {- email: profile._json.email,- username: profile.displayName,- image: profile._json.picture,+ image: (profile.photos ?? [])[0]?.value,provider: profile.provider,};const newUser = await genericCreateAccount(userData);