152
apps/builder/src/features/auth/api/customAdapter.ts
Normal file
152
apps/builder/src/features/auth/api/customAdapter.ts
Normal file
@ -0,0 +1,152 @@
|
||||
// Forked from https://github.com/nextauthjs/adapters/blob/main/packages/prisma/src/index.ts
|
||||
import {
|
||||
PrismaClient,
|
||||
Prisma,
|
||||
WorkspaceRole,
|
||||
Session,
|
||||
} from '@typebot.io/prisma'
|
||||
import type { Adapter, AdapterUser } from 'next-auth/adapters'
|
||||
import { createId } from '@paralleldrive/cuid2'
|
||||
import { generateId } from '@typebot.io/lib'
|
||||
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
|
||||
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
|
||||
import { convertInvitationsToCollaborations } from '@/features/auth/helpers/convertInvitationsToCollaborations'
|
||||
import { getNewUserInvitations } from '@/features/auth/helpers/getNewUserInvitations'
|
||||
import { joinWorkspaces } from '@/features/auth/helpers/joinWorkspaces'
|
||||
import { parseWorkspaceDefaultPlan } from '@/features/workspace/helpers/parseWorkspaceDefaultPlan'
|
||||
|
||||
export function customAdapter(p: PrismaClient): Adapter {
|
||||
return {
|
||||
createUser: async (data: Omit<AdapterUser, 'id'>) => {
|
||||
if (!data.email)
|
||||
throw Error('Provider did not forward email but it is required')
|
||||
const user = { id: createId(), email: data.email as string }
|
||||
const { invitations, workspaceInvitations } = await getNewUserInvitations(
|
||||
p,
|
||||
user.email
|
||||
)
|
||||
if (
|
||||
process.env.DISABLE_SIGNUP === 'true' &&
|
||||
process.env.ADMIN_EMAIL !== user.email &&
|
||||
invitations.length === 0 &&
|
||||
workspaceInvitations.length === 0
|
||||
)
|
||||
throw Error('New users are forbidden')
|
||||
|
||||
const newWorkspaceData = {
|
||||
name: data.name ? `${data.name}'s workspace` : `My workspace`,
|
||||
plan: parseWorkspaceDefaultPlan(data.email),
|
||||
}
|
||||
const createdUser = await p.user.create({
|
||||
data: {
|
||||
...data,
|
||||
id: user.id,
|
||||
apiTokens: {
|
||||
create: { name: 'Default', token: generateId(24) },
|
||||
},
|
||||
workspaces:
|
||||
workspaceInvitations.length > 0
|
||||
? undefined
|
||||
: {
|
||||
create: {
|
||||
role: WorkspaceRole.ADMIN,
|
||||
workspace: {
|
||||
create: newWorkspaceData,
|
||||
},
|
||||
},
|
||||
},
|
||||
onboardingCategories: [],
|
||||
},
|
||||
include: {
|
||||
workspaces: { select: { workspaceId: true } },
|
||||
},
|
||||
})
|
||||
const newWorkspaceId = createdUser.workspaces.pop()?.workspaceId
|
||||
const events: TelemetryEvent[] = []
|
||||
if (newWorkspaceId) {
|
||||
events.push({
|
||||
name: 'Workspace created',
|
||||
workspaceId: newWorkspaceId,
|
||||
userId: createdUser.id,
|
||||
data: newWorkspaceData,
|
||||
})
|
||||
}
|
||||
events.push({
|
||||
name: 'User created',
|
||||
userId: createdUser.id,
|
||||
data: {
|
||||
email: data.email,
|
||||
name: data.name ? (data.name as string).split(' ')[0] : undefined,
|
||||
},
|
||||
})
|
||||
await sendTelemetryEvents(events)
|
||||
if (invitations.length > 0)
|
||||
await convertInvitationsToCollaborations(p, user, invitations)
|
||||
if (workspaceInvitations.length > 0)
|
||||
await joinWorkspaces(p, user, workspaceInvitations)
|
||||
return createdUser as AdapterUser
|
||||
},
|
||||
getUser: async (id) =>
|
||||
(await p.user.findUnique({ where: { id } })) as AdapterUser,
|
||||
getUserByEmail: async (email) =>
|
||||
(await p.user.findUnique({ where: { email } })) as AdapterUser,
|
||||
async getUserByAccount(provider_providerAccountId) {
|
||||
const account = await p.account.findUnique({
|
||||
where: { provider_providerAccountId },
|
||||
select: { user: true },
|
||||
})
|
||||
return (account?.user ?? null) as AdapterUser | null
|
||||
},
|
||||
updateUser: async (data) =>
|
||||
(await p.user.update({ where: { id: data.id }, data })) as AdapterUser,
|
||||
deleteUser: async (id) =>
|
||||
(await p.user.delete({ where: { id } })) as AdapterUser,
|
||||
linkAccount: async (data) => {
|
||||
await p.account.create({
|
||||
data: {
|
||||
userId: data.userId,
|
||||
type: data.type,
|
||||
provider: data.provider,
|
||||
providerAccountId: data.providerAccountId,
|
||||
refresh_token: data.refresh_token,
|
||||
access_token: data.access_token,
|
||||
expires_at: data.expires_at,
|
||||
token_type: data.token_type,
|
||||
scope: data.scope,
|
||||
id_token: data.id_token,
|
||||
session_state: data.session_state,
|
||||
oauth_token_secret: data.oauth_token_secret as string,
|
||||
oauth_token: data.oauth_token as string,
|
||||
refresh_token_expires_in: data.refresh_token_expires_in as number,
|
||||
},
|
||||
})
|
||||
},
|
||||
unlinkAccount: async (provider_providerAccountId) => {
|
||||
await p.account.delete({ where: { provider_providerAccountId } })
|
||||
},
|
||||
async getSessionAndUser(sessionToken) {
|
||||
const userAndSession = await p.session.findUnique({
|
||||
where: { sessionToken },
|
||||
include: { user: true },
|
||||
})
|
||||
if (!userAndSession) return null
|
||||
const { user, ...session } = userAndSession
|
||||
return { user, session } as { user: AdapterUser; session: Session }
|
||||
},
|
||||
createSession: (data) => p.session.create({ data }),
|
||||
updateSession: (data) =>
|
||||
p.session.update({ data, where: { sessionToken: data.sessionToken } }),
|
||||
deleteSession: (sessionToken) =>
|
||||
p.session.delete({ where: { sessionToken } }),
|
||||
createVerificationToken: (data) => p.verificationToken.create({ data }),
|
||||
async useVerificationToken(identifier_token) {
|
||||
try {
|
||||
return await p.verificationToken.delete({ where: { identifier_token } })
|
||||
} catch (error) {
|
||||
if ((error as Prisma.PrismaClientKnownRequestError).code === 'P2025')
|
||||
return null
|
||||
throw error
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
@ -1,24 +1,25 @@
|
||||
import { useScopedI18n } from '@/locales'
|
||||
import { Alert } from '@chakra-ui/react'
|
||||
|
||||
type Props = {
|
||||
error: string
|
||||
}
|
||||
const errors: Record<string, string> = {
|
||||
Signin: 'Try signing with a different account.',
|
||||
OAuthSignin: 'Try signing with a different account.',
|
||||
OAuthCallback: 'Try signing with a different account.',
|
||||
OAuthCreateAccount: 'Email not found. Try signing with a different provider.',
|
||||
EmailCreateAccount: 'Try signing with a different account.',
|
||||
Callback: 'Try signing with a different account.',
|
||||
OAuthAccountNotLinked:
|
||||
'To confirm your identity, sign in with the same account you used originally.',
|
||||
CredentialsSignin:
|
||||
'Sign in failed. Check the details you provided are correct.',
|
||||
default: 'An error occurred. Please try again.',
|
||||
}
|
||||
|
||||
export const SignInError = ({ error }: Props) => (
|
||||
<Alert status="error" variant="solid" rounded="md">
|
||||
{errors[error] ?? errors[error]}
|
||||
</Alert>
|
||||
)
|
||||
export const SignInError = ({ error }: Props) => {
|
||||
const scopedT = useScopedI18n('auth.error')
|
||||
const errors: Record<string, string> = {
|
||||
Signin: scopedT('default'),
|
||||
OAuthSignin: scopedT('default'),
|
||||
OAuthCallback: scopedT('default'),
|
||||
OAuthCreateAccount: scopedT('email'),
|
||||
EmailCreateAccount: scopedT('default'),
|
||||
Callback: scopedT('default'),
|
||||
OAuthAccountNotLinked: scopedT('oauthNotLinked'),
|
||||
default: scopedT('unknown'),
|
||||
}
|
||||
return (
|
||||
<Alert status="error" variant="solid" rounded="md">
|
||||
{errors[error] ?? errors[error]}
|
||||
</Alert>
|
||||
)
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import { BuiltInProviderType } from 'next-auth/providers'
|
||||
import { useToast } from '@/hooks/useToast'
|
||||
import { TextLink } from '@/components/TextLink'
|
||||
import { SignInError } from './SignInError'
|
||||
import { useScopedI18n } from '@/locales'
|
||||
|
||||
type Props = {
|
||||
defaultEmail?: string
|
||||
@ -34,6 +35,7 @@ type Props = {
|
||||
export const SignInForm = ({
|
||||
defaultEmail,
|
||||
}: Props & HTMLChakraProps<'form'>) => {
|
||||
const scopedT = useScopedI18n('auth')
|
||||
const router = useRouter()
|
||||
const { status } = useSession()
|
||||
const [authLoading, setAuthLoading] = useState(false)
|
||||
@ -76,8 +78,8 @@ export const SignInForm = ({
|
||||
})
|
||||
if (response?.error) {
|
||||
showToast({
|
||||
title: 'Unauthorized',
|
||||
description: 'Sign ups are disabled.',
|
||||
title: scopedT('signinErrorToast.title'),
|
||||
description: scopedT('signinErrorToast.description'),
|
||||
})
|
||||
} else {
|
||||
setIsMagicLinkSent(true)
|
||||
@ -89,14 +91,13 @@ export const SignInForm = ({
|
||||
if (hasNoAuthProvider)
|
||||
return (
|
||||
<Text>
|
||||
You need to{' '}
|
||||
{scopedT('noProvider.preLink')}{' '}
|
||||
<TextLink
|
||||
href="https://docs.typebot.io/self-hosting/configuration"
|
||||
isExternal
|
||||
>
|
||||
configure at least one auth provider
|
||||
</TextLink>{' '}
|
||||
(Email, Google, GitHub, Facebook or Azure AD).
|
||||
{scopedT('noProvider.link')}
|
||||
</TextLink>
|
||||
</Text>
|
||||
)
|
||||
return (
|
||||
@ -106,7 +107,9 @@ export const SignInForm = ({
|
||||
<SocialLoginButtons providers={providers} />
|
||||
{providers?.email && (
|
||||
<>
|
||||
<DividerWithText mt="6">Or with your email</DividerWithText>
|
||||
<DividerWithText mt="6">
|
||||
{scopedT('orEmailLabel')}
|
||||
</DividerWithText>
|
||||
<HStack as="form" onSubmit={handleEmailSubmit}>
|
||||
<Input
|
||||
name="email"
|
||||
@ -124,7 +127,7 @@ export const SignInForm = ({
|
||||
}
|
||||
isDisabled={isMagicLinkSent}
|
||||
>
|
||||
Submit
|
||||
{scopedT('emailSubmitButton.label')}
|
||||
</Button>
|
||||
</HStack>
|
||||
</>
|
||||
@ -140,10 +143,8 @@ export const SignInForm = ({
|
||||
<HStack>
|
||||
<AlertIcon />
|
||||
<Stack spacing={1}>
|
||||
<Text fontWeight="semibold">
|
||||
A magic link email was sent. 🪄
|
||||
</Text>
|
||||
<Text fontSize="sm">Make sure to check your SPAM folder.</Text>
|
||||
<Text fontWeight="semibold">{scopedT('magicLink.title')}</Text>
|
||||
<Text fontSize="sm">{scopedT('magicLink.description')}</Text>
|
||||
</Stack>
|
||||
</HStack>
|
||||
</Alert>
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Seo } from '@/components/Seo'
|
||||
import { TextLink } from '@/components/TextLink'
|
||||
import { useScopedI18n } from '@/locales'
|
||||
import { VStack, Heading, Text } from '@chakra-ui/react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { SignInForm } from './SignInForm'
|
||||
@ -10,26 +11,40 @@ type Props = {
|
||||
}
|
||||
|
||||
export const SignInPage = ({ type }: Props) => {
|
||||
const scopedT = useScopedI18n('auth')
|
||||
const { query } = useRouter()
|
||||
|
||||
return (
|
||||
<VStack spacing={4} h="100vh" justifyContent="center">
|
||||
<Seo title={type === 'signin' ? 'Sign In' : 'Register'} />
|
||||
<Seo
|
||||
title={
|
||||
type === 'signin'
|
||||
? scopedT('signin.heading')
|
||||
: scopedT('register.heading')
|
||||
}
|
||||
/>
|
||||
<Heading
|
||||
onClick={() => {
|
||||
throw new Error('Sentry is working')
|
||||
}}
|
||||
>
|
||||
{type === 'signin' ? 'Sign In' : 'Create an account'}
|
||||
{type === 'signin'
|
||||
? scopedT('signin.heading')
|
||||
: scopedT('register.heading')}
|
||||
</Heading>
|
||||
{type === 'signin' ? (
|
||||
<Text>
|
||||
Don't have an account?{' '}
|
||||
<TextLink href="/register">Sign up for free</TextLink>
|
||||
{scopedT('signin.noAccountLabel.preLink')}{' '}
|
||||
<TextLink href="/register">
|
||||
{scopedT('signin.noAccountLabel.link')}
|
||||
</TextLink>
|
||||
</Text>
|
||||
) : (
|
||||
<Text>
|
||||
Already have an account? <TextLink href="/signin">Sign in</TextLink>
|
||||
{scopedT('register.alreadyHaveAccountLabel.preLink')}{' '}
|
||||
<TextLink href="/signin">
|
||||
{scopedT('register.alreadyHaveAccountLabel.link')}
|
||||
</TextLink>
|
||||
</Text>
|
||||
)}
|
||||
<SignInForm defaultEmail={query.g?.toString()} />
|
||||
|
@ -15,6 +15,7 @@ import { omit } from '@typebot.io/lib'
|
||||
import { AzureAdLogo } from '@/components/logos/AzureAdLogo'
|
||||
import { FacebookLogo } from '@/components/logos/FacebookLogo'
|
||||
import { GitlabLogo } from '@/components/logos/GitlabLogo'
|
||||
import { useScopedI18n } from '@/locales'
|
||||
|
||||
type Props = {
|
||||
providers:
|
||||
@ -23,6 +24,7 @@ type Props = {
|
||||
}
|
||||
|
||||
export const SocialLoginButtons = ({ providers }: Props) => {
|
||||
const scopedT = useScopedI18n('auth.socialLogin')
|
||||
const { query } = useRouter()
|
||||
const { status } = useSession()
|
||||
const [authLoading, setAuthLoading] =
|
||||
@ -63,7 +65,7 @@ export const SocialLoginButtons = ({ providers }: Props) => {
|
||||
}
|
||||
variant="outline"
|
||||
>
|
||||
Continue with GitHub
|
||||
{scopedT('githubButton.label')}
|
||||
</Button>
|
||||
)}
|
||||
{providers?.google && (
|
||||
@ -77,7 +79,7 @@ export const SocialLoginButtons = ({ providers }: Props) => {
|
||||
}
|
||||
variant="outline"
|
||||
>
|
||||
Continue with Google
|
||||
{scopedT('googleButton.label')}
|
||||
</Button>
|
||||
)}
|
||||
{providers?.facebook && (
|
||||
@ -91,7 +93,7 @@ export const SocialLoginButtons = ({ providers }: Props) => {
|
||||
}
|
||||
variant="outline"
|
||||
>
|
||||
Continue with Facebook
|
||||
{scopedT('facebookButton.label')}
|
||||
</Button>
|
||||
)}
|
||||
{providers?.gitlab && (
|
||||
@ -105,7 +107,9 @@ export const SocialLoginButtons = ({ providers }: Props) => {
|
||||
}
|
||||
variant="outline"
|
||||
>
|
||||
Continue with {providers.gitlab.name}
|
||||
{scopedT('gitlabButton.label', {
|
||||
gitlabProviderName: providers.gitlab.name,
|
||||
})}
|
||||
</Button>
|
||||
)}
|
||||
{providers?.['azure-ad'] && (
|
||||
@ -119,7 +123,9 @@ export const SocialLoginButtons = ({ providers }: Props) => {
|
||||
}
|
||||
variant="outline"
|
||||
>
|
||||
Continue with {providers['azure-ad'].name}
|
||||
{scopedT('azureButton.label', {
|
||||
azureProviderName: providers['azure-ad'].name,
|
||||
})}
|
||||
</Button>
|
||||
)}
|
||||
{providers?.['custom-oauth'] && (
|
||||
@ -131,7 +137,9 @@ export const SocialLoginButtons = ({ providers }: Props) => {
|
||||
}
|
||||
variant="outline"
|
||||
>
|
||||
Continue with {providers['custom-oauth'].name}
|
||||
{scopedT('customButton.label', {
|
||||
customProviderName: providers['custom-oauth'].name,
|
||||
})}
|
||||
</Button>
|
||||
)}
|
||||
</Stack>
|
||||
|
@ -0,0 +1,14 @@
|
||||
import { sendMagicLinkEmail } from '@typebot.io/emails'
|
||||
|
||||
type Props = {
|
||||
identifier: string
|
||||
url: string
|
||||
}
|
||||
|
||||
export const sendVerificationRequest = async ({ identifier, url }: Props) => {
|
||||
try {
|
||||
await sendMagicLinkEmail({ url, to: identifier })
|
||||
} catch (err) {
|
||||
throw new Error(`Email(s) could not be sent`)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user