2023-04-03 16:42:10 +02:00
|
|
|
import { GetServerSidePropsContext } from 'next'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { User } from '@typebot.io/prisma'
|
|
|
|
import { isNotDefined } from '@typebot.io/lib'
|
2022-03-07 14:35:35 +01:00
|
|
|
import { sign } from 'jsonwebtoken'
|
2023-04-03 16:42:10 +02:00
|
|
|
import { getServerSession } from 'next-auth'
|
|
|
|
import { authOptions } from './api/auth/[...nextauth]'
|
2023-08-28 09:13:53 +02:00
|
|
|
import { env } from '@typebot.io/env'
|
2022-03-07 14:35:35 +01:00
|
|
|
|
2022-11-15 09:35:48 +01:00
|
|
|
export default function Page() {
|
|
|
|
return null
|
2022-03-07 14:35:35 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:42:10 +02:00
|
|
|
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
|
|
|
const session = await getServerSession(context.req, context.res, authOptions)
|
2022-03-07 14:35:35 +01:00
|
|
|
if (isNotDefined(session?.user))
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
permanent: false,
|
|
|
|
destination: `/signin?redirectPath=%2Ffeedback`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const sleekplanToken = createSSOToken(session?.user as User)
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
permanent: false,
|
|
|
|
destination: `https://feedback.typebot.io?sso=${sleekplanToken}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createSSOToken = (user: User) => {
|
2023-08-28 09:13:53 +02:00
|
|
|
if (!env.SLEEKPLAN_SSO_KEY) return
|
2022-03-07 14:35:35 +01:00
|
|
|
const userData = {
|
|
|
|
mail: user.email,
|
|
|
|
id: user.id,
|
|
|
|
name: user.name,
|
|
|
|
img: user.image,
|
|
|
|
}
|
|
|
|
|
2023-08-28 09:13:53 +02:00
|
|
|
return sign(userData, env.SLEEKPLAN_SSO_KEY, { algorithm: 'HS256' })
|
2022-03-07 14:35:35 +01:00
|
|
|
}
|