From 57663fd05c5ffe84afc117f41d1429902ee77674 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 7 Mar 2022 10:40:09 +0100 Subject: [PATCH] =?UTF-8?q?build(signin):=20=E2=99=BF=EF=B8=8F=20Add=20SSO?= =?UTF-8?q?=20signin=20for=20Sleekplan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/builder/package.json | 2 ++ apps/builder/pages/api/auth/sleekplan.ts | 31 ++++++++++++++++++++++++ apps/builder/pages/typebots.tsx | 13 +++++++--- yarn.lock | 7 ++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 apps/builder/pages/api/auth/sleekplan.ts diff --git a/apps/builder/package.json b/apps/builder/package.json index 7434b232a..309670b86 100644 --- a/apps/builder/package.json +++ b/apps/builder/package.json @@ -51,6 +51,7 @@ "htmlparser2": "^7.2.0", "immer": "^9.0.12", "js-video-url-parser": "^0.5.1", + "jsonwebtoken": "^8.5.1", "kbar": "^0.1.0-beta.27", "micro": "^9.3.4", "micro-cors": "^0.1.1", @@ -84,6 +85,7 @@ "devDependencies": { "@playwright/test": "^1.19.0", "@types/google-spreadsheet": "^3.1.5", + "@types/jsonwebtoken": "8.5.1", "@types/micro-cors": "^0.1.2", "@types/minio": "^7.0.12", "@types/node": "^17.0.17", diff --git a/apps/builder/pages/api/auth/sleekplan.ts b/apps/builder/pages/api/auth/sleekplan.ts new file mode 100644 index 000000000..5274a56e8 --- /dev/null +++ b/apps/builder/pages/api/auth/sleekplan.ts @@ -0,0 +1,31 @@ +import { withSentry } from '@sentry/nextjs' +import { User } from 'db' +import { sign } from 'jsonwebtoken' +import { NextApiRequest, NextApiResponse } from 'next' +import { getAuthenticatedUser } from 'services/api/utils' +import { methodNotAllowed, notAuthenticated } from 'utils' + +const handler = async (req: NextApiRequest, res: NextApiResponse) => { + if (req.method === 'GET') { + const user = await getAuthenticatedUser(req) + if (!user) return notAuthenticated(res) + const ssoToken = createSSOToken(user) + res.redirect(`https://feedback.typebot.io?sso=${ssoToken}`) + return + } + methodNotAllowed(res) +} + +const createSSOToken = (user: User) => { + if (!process.env.SLEEKPLAN_SSO_KEY) return + const userData = { + mail: user.email, + id: user.id, + name: user.name, + img: user.image, + } + + return sign(userData, process.env.SLEEKPLAN_SSO_KEY, { algorithm: 'HS256' }) +} + +export default withSentry(handler) diff --git a/apps/builder/pages/typebots.tsx b/apps/builder/pages/typebots.tsx index ca63e608a..eb05e733d 100644 --- a/apps/builder/pages/typebots.tsx +++ b/apps/builder/pages/typebots.tsx @@ -1,5 +1,5 @@ import React, { useEffect, useState } from 'react' -import { Flex, Link, Stack, Text } from '@chakra-ui/layout' +import { Link, Stack, Text, VStack } from '@chakra-ui/layout' import { DashboardHeader } from 'components/dashboard/DashboardHeader' import { Seo } from 'components/Seo' import { FolderContent } from 'components/dashboard/FolderContent' @@ -35,8 +35,14 @@ const DashboardPage = () => { if (!isReady) return const couponCode = query.coupon?.toString() const stripeStatus = query.stripe?.toString() + const sleekplan = query.sleekplan?.toString() const redirectPath = query.redirectPath as string | undefined + if (sleekplan) { + setIsLoading(true) + push('/api/auth/sleekplan') + return + } if (stripeStatus === 'success') toast({ title: 'Typebot Pro', @@ -66,9 +72,10 @@ const DashboardPage = () => { {isLoading ? ( - + + You are being redirected... - + ) : ( )} diff --git a/yarn.lock b/yarn.lock index 17edd707e..e08ae21b2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3971,6 +3971,13 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= +"@types/jsonwebtoken@8.5.1": + version "8.5.1" + resolved "https://registry.yarnpkg.com/@types/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz#56958cb2d80f6d74352bd2e501a018e2506a8a84" + integrity sha512-rNAPdomlIUX0i0cg2+I+Q1wOUr531zHBQ+cV/28PJ39bSPKjahatZZ2LMuhiguETkCgLVzfruw/ZvNMNkKoSzw== + dependencies: + "@types/node" "*" + "@types/keyv@*": version "3.1.3" resolved "https://registry.yarnpkg.com/@types/keyv/-/keyv-3.1.3.tgz#1c9aae32872ec1f20dcdaee89a9f3ba88f465e41"