Add authentication
This commit is contained in:
21
apps/builder/pages/_app.tsx
Normal file
21
apps/builder/pages/_app.tsx
Normal file
@ -0,0 +1,21 @@
|
||||
import React from 'react'
|
||||
import { AppProps } from 'next/app'
|
||||
import { SessionProvider } from 'next-auth/react'
|
||||
import { ChakraProvider } from '@chakra-ui/react'
|
||||
import { customTheme } from 'libs/chakra'
|
||||
import 'assets/styles/routerProgressBar.css'
|
||||
import { useRouterProgressBar } from 'libs/routerProgressBar'
|
||||
|
||||
const App = ({ Component, pageProps }: AppProps) => {
|
||||
useRouterProgressBar()
|
||||
|
||||
return (
|
||||
<ChakraProvider theme={customTheme}>
|
||||
<SessionProvider>
|
||||
<Component {...pageProps} />
|
||||
</SessionProvider>
|
||||
</ChakraProvider>
|
||||
)
|
||||
}
|
||||
|
||||
export default App
|
36
apps/builder/pages/_document.tsx
Normal file
36
apps/builder/pages/_document.tsx
Normal file
@ -0,0 +1,36 @@
|
||||
import Document, {
|
||||
Html,
|
||||
Head,
|
||||
Main,
|
||||
NextScript,
|
||||
DocumentContext,
|
||||
} from 'next/document'
|
||||
|
||||
class MyDocument extends Document {
|
||||
static async getInitialProps(ctx: DocumentContext) {
|
||||
const initialProps = await Document.getInitialProps(ctx)
|
||||
return { ...initialProps }
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Html>
|
||||
<Head>
|
||||
<link rel="icon" type="image/png" href="/favicon.png" />
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" />
|
||||
<link
|
||||
href="https://fonts.googleapis.com/css2?family=Outfit:wght@400;500;600;700&family=Open+Sans:wght@400;500;600;700&display=swap"
|
||||
rel="stylesheet"
|
||||
/>
|
||||
</Head>
|
||||
<body>
|
||||
<Main />
|
||||
<NextScript />
|
||||
</body>
|
||||
</Html>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default MyDocument
|
39
apps/builder/pages/api/auth/[...nextauth].ts
Normal file
39
apps/builder/pages/api/auth/[...nextauth].ts
Normal file
@ -0,0 +1,39 @@
|
||||
import NextAuth from 'next-auth'
|
||||
import { PrismaAdapter } from '@next-auth/prisma-adapter'
|
||||
import { PrismaClient } from '@typebot/prisma'
|
||||
import EmailProvider from 'next-auth/providers/email'
|
||||
import GitHubProvider from 'next-auth/providers/github'
|
||||
import GoogleProvider from 'next-auth/providers/google'
|
||||
import FacebookProvider from 'next-auth/providers/facebook'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export default NextAuth({
|
||||
adapter: PrismaAdapter(prisma),
|
||||
secret: process.env.SECRET,
|
||||
providers: [
|
||||
EmailProvider({
|
||||
server: {
|
||||
host: process.env.EMAIL_SERVER_HOST,
|
||||
port: process.env.EMAIL_SERVER_PORT,
|
||||
auth: {
|
||||
user: process.env.EMAIL_SERVER_USER,
|
||||
pass: process.env.EMAIL_SERVER_PASSWORD,
|
||||
},
|
||||
},
|
||||
from: process.env.EMAIL_FROM,
|
||||
}),
|
||||
GitHubProvider({
|
||||
clientId: process.env.GITHUB_CLIENT_ID,
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
||||
}),
|
||||
GoogleProvider({
|
||||
clientId: process.env.GOOGLE_CLIENT_ID ?? '',
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? '',
|
||||
}),
|
||||
FacebookProvider({
|
||||
clientId: process.env.FACEBOOK_CLIENT_ID ?? '',
|
||||
clientSecret: process.env.FACEBOOK_CLIENT_SECRET ?? '',
|
||||
}),
|
||||
],
|
||||
})
|
18
apps/builder/pages/index.tsx
Normal file
18
apps/builder/pages/index.tsx
Normal file
@ -0,0 +1,18 @@
|
||||
import { GetServerSidePropsContext } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
|
||||
function RedirectPage() {
|
||||
return
|
||||
}
|
||||
|
||||
export const getServerSideProps = async (
|
||||
context: GetServerSidePropsContext
|
||||
) => {
|
||||
const session = await getSession(context)
|
||||
if (!session?.user) {
|
||||
return { redirect: { permanent: false, destination: '/signin' } }
|
||||
}
|
||||
return { redirect: { permanent: false, destination: '/typebots' } }
|
||||
}
|
||||
|
||||
export default RedirectPage
|
19
apps/builder/pages/register.tsx
Normal file
19
apps/builder/pages/register.tsx
Normal file
@ -0,0 +1,19 @@
|
||||
import { AuthSwitcher } from 'components/auth/AuthSwitcher'
|
||||
import { SignInForm } from 'components/auth/SignInForm'
|
||||
import { Heading, VStack } from '@chakra-ui/react'
|
||||
import { useRouter } from 'next/router'
|
||||
import React from 'react'
|
||||
|
||||
const RegisterPage = () => {
|
||||
const { query } = useRouter()
|
||||
|
||||
return (
|
||||
<VStack spacing={4} h="100vh" justifyContent="center">
|
||||
<Heading>Create an account</Heading>
|
||||
<AuthSwitcher type="register" />
|
||||
<SignInForm defaultEmail={query.g?.toString()} />
|
||||
</VStack>
|
||||
)
|
||||
}
|
||||
|
||||
export default RegisterPage
|
16
apps/builder/pages/signin.tsx
Normal file
16
apps/builder/pages/signin.tsx
Normal file
@ -0,0 +1,16 @@
|
||||
import { AuthSwitcher } from 'components/auth/AuthSwitcher'
|
||||
import { SignInForm } from 'components/auth/SignInForm'
|
||||
import { Heading, VStack } from '@chakra-ui/react'
|
||||
import React from 'react'
|
||||
|
||||
const SignInPage = () => {
|
||||
return (
|
||||
<VStack spacing={4} h="100vh" justifyContent="center">
|
||||
<Heading>Sign in</Heading>
|
||||
<AuthSwitcher type="signin" />
|
||||
<SignInForm />
|
||||
</VStack>
|
||||
)
|
||||
}
|
||||
|
||||
export default SignInPage
|
9
apps/builder/pages/typebots/index.tsx
Normal file
9
apps/builder/pages/typebots/index.tsx
Normal file
@ -0,0 +1,9 @@
|
||||
import withAuth, { withAuthProps } from 'components/HOC/withUser'
|
||||
import { Text } from '@chakra-ui/react'
|
||||
import React from 'react'
|
||||
|
||||
const TypebotsPage = ({ user }: withAuthProps) => {
|
||||
return <Text data-testid="authenticated">Hello {user?.email}</Text>
|
||||
}
|
||||
|
||||
export default withAuth(TypebotsPage)
|
Reference in New Issue
Block a user