2
0

build(signin): ️ Add SSO signin for Sleekplan

This commit is contained in:
Baptiste Arnaud
2022-03-07 10:40:09 +01:00
parent 07104038c4
commit 57663fd05c
4 changed files with 50 additions and 3 deletions

View File

@ -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)