import { Button, HTMLChakraProps, Input, Stack, HStack, useToast, Text, } from '@chakra-ui/react' import React, { ChangeEvent, FormEvent, useEffect } from 'react' import { useState } from 'react' import { ClientSafeProvider, getProviders, LiteralUnion, signIn, useSession, } from 'next-auth/react' import { DividerWithText } from './DividerWithText' import { SocialLoginButtons } from './SocialLoginButtons' import { useRouter } from 'next/router' import { NextChakraLink } from 'components/nextChakra/NextChakraLink' import { BuiltInProviderType } from 'next-auth/providers' type Props = { defaultEmail?: string } export const SignInForm = ({ defaultEmail, }: Props & HTMLChakraProps<'form'>) => { const router = useRouter() const { status } = useSession() const [authLoading, setAuthLoading] = useState(false) const [isLoadingProviders, setIsLoadingProviders] = useState(true) const [emailValue, setEmailValue] = useState(defaultEmail ?? '') const toast = useToast({ position: 'top-right', }) const [providers, setProviders] = useState< Record, ClientSafeProvider> >() const hasNoAuthProvider = !isLoadingProviders && Object.keys(providers ?? {}).length === 0 useEffect(() => { if (status === 'authenticated') router.replace({ pathname: '/typebots', query: router.query }) ;(async () => { const providers = await getProviders() setProviders(providers ?? undefined) setIsLoadingProviders(false) })() }, [status, router]) const handleEmailChange = (e: ChangeEvent) => setEmailValue(e.target.value) const handleEmailSubmit = async (e: FormEvent) => { e.preventDefault() setAuthLoading(true) await signIn('email', { email: emailValue, redirect: false, }) toast({ status: 'success', title: 'Success!', description: 'Check your inbox to sign in', }) setAuthLoading(false) } if (hasNoAuthProvider) return ( You need to{' '} configure at least one auth provider {' '} (Email, Google, GitHub or Facebook). ) return ( {providers?.email && ( <> Or with your email )} ) }