Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add login/registration forms #284

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/webapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import DashboardPage from './components/dashboard';
import useProfileStore from './state/profileStore';
import { usePostHog } from 'posthog-js/react';
import { useEffect } from 'react';
import RegisterPage from './routes/auth_.register';
import LoginPage from './routes/auth_.login';

const queryClient = new QueryClient();

Expand All @@ -34,6 +36,9 @@ function App() {
<ThemeProvider defaultTheme='dark' storageKey='vite-ui-theme'>
<Router>
<Routes>
<Route path='/auth/register' element={<RegisterPage />} />
<Route path='/auth/login' element={<LoginPage />} />

<Route path='/' element={<RootLayout />}>
<Route index element={<ConnectionsPage />} />
<Route path='/dashboard' element={<DashboardPage />} />
Expand Down
Binary file added apps/webapp/src/assets/login.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/webapp/src/assets/register.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions apps/webapp/src/components/auth/login/login-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from 'zod';
import { registerSchema } from '../register/register-schema';

export const loginSchema = registerSchema.pick({
email: true,
password: true,
});

export type LoginSchemaType = z.infer<typeof loginSchema>;
90 changes: 90 additions & 0 deletions apps/webapp/src/components/auth/login/login-user-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { useForm } from 'react-hook-form';
import { Link, useNavigate } from 'react-router-dom';
import { zodResolver } from '@hookform/resolvers/zod';

import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { LoadingSpinner } from '@/components/connections/components/LoadingSpinner';

import useLoginMutation from '@/hooks/mutations/useLoginMutation';
import { type LoginSchemaType, loginSchema } from './login-schema';

export const LoginUserForm = () => {
const navigate = useNavigate();
const { mutate: login, isPending: loginPending } = useLoginMutation();

const form = useForm<LoginSchemaType>({
resolver: zodResolver(loginSchema),
});

const onSubmit = async (data: LoginSchemaType) => {
const loginData = {
email: data.email,
password: data.password,
};

login(loginData, {
onSuccess: () => {
navigate('/');
},
});
};

return (
<div className='mx-auto w-full max-w-sm lg:w-96'>
<div className='text-center'>
<Link to='/'>
<img src='/logo.png' className='w-14 mx-auto' />
</Link>
<h2 className='mt-6 text-3xl font-extrabold'>Login</h2>
<p className='mt-2 text-sm'>
Don&apos;t have an account?{' '}
<Link to='/auth/register' className='font-medium text-primary hover:text-primary/80'>
Register
</Link>
</p>
</div>

<Form {...form}>
<form className='space-y-6 mt-8' onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name='email'
render={({ field }) => (
<FormItem>
<FormLabel htmlFor='email'>Email</FormLabel>
<FormControl>
<Input placeholder='[email protected]' autoComplete='email' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name='password'
render={({ field }) => (
<FormItem>
<FormLabel htmlFor='password'>Password</FormLabel>
<FormControl>
<Input placeholder='********' type='password' autoComplete='current-password' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<Link to='/auth/forgot-password' className='font-medium block text-sm text-primary hover:text-primary/80'>
Forgot your password?
</Link>

<Button type='submit' className='w-full' disabled={loginPending}>
{loginPending ? <LoadingSpinner className='' /> : 'Login'}
</Button>
</form>
</Form>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useForm } from "react-hook-form";
import { Link, useNavigate } from "react-router-dom";
import { zodResolver } from "@hookform/resolvers/zod";

import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { LoadingSpinner } from "@/components/connections/components/LoadingSpinner";

import useOrganisationMutation from "@/hooks/mutations/useOrganisationMutation";
import {
type OrganizationFormSchemaType,
organizationFormSchema,
} from "./create-organization-schema";

export const CreateOrganizationForm = () => {
const navigate = useNavigate();
const { mutate, isPending } = useOrganisationMutation();

const form = useForm<OrganizationFormSchemaType>({
resolver: zodResolver(organizationFormSchema),
});

const onSubmit = (data: OrganizationFormSchemaType) => {
mutate(
{ ...data, stripe_customer_id: "stripe-customer-76" },
{
onSuccess: () => navigate("/"),
}
);
};

return (
<div className="mx-auto w-full max-w-sm lg:w-96">
<div className="text-center">
<Link to="/">
<img src="/logo.png" className="w-14 mx-auto" />
</Link>
<h2 className="mt-6 text-3xl font-extrabold">Create Organization</h2>
</div>

<Form {...form}>
<form className="space-y-6 mt-8" onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="name">Name</FormLabel>
<FormControl>
<Input
placeholder="organization"
autoComplete="given-name"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button className="w-full" type="submit">
{isPending ? <LoadingSpinner className="" /> : "Create"}
</Button>
</form>
</Form>
<Link
to="/"
className="mt-4 text-sm font-medium text-primary hover:text-primary/80 text-center block mx-auto"
>
Skip For Now
</Link>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod';

export const organizationFormSchema = z.object({
name: z.string().min(1, 'Name is required'),
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding more validation rules to the name field in organizationFormSchema to ensure data integrity, such as a maximum length constraint.

-  name: z.string().min(1, 'Name is required'),
+  name: z.string().min(1, 'Name is required').max(100, 'Name must be less than 100 characters'),

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const organizationFormSchema = z.object({
name: z.string().min(1, 'Name is required'),
});
export const organizationFormSchema = z.object({
name: z.string().min(1, 'Name is required').max(100, 'Name must be less than 100 characters'),
});


export type OrganizationFormSchemaType = z.infer<typeof organizationFormSchema>;
12 changes: 12 additions & 0 deletions apps/webapp/src/components/auth/register/register-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState } from 'react';

import { RegisterUserForm } from './register-user-form';
import { CreateOrganizationForm } from './create-organization-form';

export const RegisterForm = () => {
const [registered, setIsRegistered] = useState(false);

if (!registered) return <RegisterUserForm onSuccess={() => setIsRegistered(true)} />;

return <CreateOrganizationForm />;
};
10 changes: 10 additions & 0 deletions apps/webapp/src/components/auth/register/register-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from 'zod';

export const registerSchema = z.object({
firstName: z.string().min(1, 'First name is required'),
lastName: z.string().min(1, 'Last name is required'),
email: z.string().min(1, 'Email is required').email('Email is invalid'),
password: z.string().min(8, 'Password must be at least 8 characters'),
});
Comment on lines +3 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The registerSchema includes basic validation for firstName, lastName, email, and password. Consider adding more comprehensive validation rules for email and password to enhance security, such as regex patterns for email format and password complexity requirements.

-  email: z.string().min(1, 'Email is required').email('Email is invalid'),
+  email: z.string().min(1, 'Email is required').email('Email is invalid').regex(/Your-Regex-Here/, 'Email format is invalid'),
-  password: z.string().min(8, 'Password must be at least 8 characters'),
+  password: z.string().min(8, 'Password must be at least 8 characters').regex(/Your-Password-Regex-Here/, 'Password format is invalid'),

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const registerSchema = z.object({
firstName: z.string().min(1, 'First name is required'),
lastName: z.string().min(1, 'Last name is required'),
email: z.string().min(1, 'Email is required').email('Email is invalid'),
password: z.string().min(8, 'Password must be at least 8 characters'),
});
export const registerSchema = z.object({
firstName: z.string().min(1, 'First name is required'),
lastName: z.string().min(1, 'Last name is required'),
email: z.string().min(1, 'Email is required').email('Email is invalid').regex(/Your-Regex-Here/, 'Email format is invalid'),
password: z.string().min(8, 'Password must be at least 8 characters').regex(/Your-Password-Regex-Here/, 'Password format is invalid'),
});


export type RegisterSchemaType = z.infer<typeof registerSchema>;
122 changes: 122 additions & 0 deletions apps/webapp/src/components/auth/register/register-user-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { Link } from 'react-router-dom';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';

import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { LoadingSpinner } from '@/components/connections/components/LoadingSpinner';

import useRegisterMutation from '@/hooks/mutations/useRegisterMutations';
import useLoginMutation from '@/hooks/mutations/useLoginMutation';
import { type RegisterSchemaType, registerSchema } from './register-schema';

export const RegisterUserForm = ({ onSuccess }: { onSuccess: () => void }) => {
const { mutate: register, isPending: registerPending } = useRegisterMutation();
const { mutateAsync: login, isPending: loginPending } = useLoginMutation();

const form = useForm<RegisterSchemaType>({
resolver: zodResolver(registerSchema),
});

const onSubmit = (data: RegisterSchemaType) => {
register(data, {
onSuccess: async () => {
const loginData = {
email: data.email,
password: data.password,
};

await login(loginData, {
onSuccess: () => {
// Change Register Step To Create Organization
onSuccess();
},
});
},
});
};

return (
<div className='mx-auto w-full max-w-sm lg:w-96'>
<div className='text-center'>
<Link to='/'>
<img src='/logo.png' className='w-14 mx-auto' />
</Link>
<h2 className='mt-6 text-3xl font-extrabold'>Create an account</h2>
<p className='mt-2 text-sm'>
Already have an account?{' '}
<Link to='/auth/login' className='font-medium text-primary hover:text-primary/80'>
Login
</Link>
</p>
</div>

<Form {...form}>
<form className='space-y-6 mt-8' onSubmit={form.handleSubmit(onSubmit)}>
<div className='grid md:grid-cols-2 gap-4'>
<FormField
control={form.control}
name='firstName'
render={({ field }) => (
<FormItem>
<FormLabel htmlFor='firstName'>First name</FormLabel>
<FormControl>
<Input placeholder='Jane' autoComplete='given-name' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name='lastName'
render={({ field }) => (
<FormItem>
<FormLabel htmlFor='lastName'>Last name</FormLabel>
<FormControl>
<Input placeholder='Doe' autoComplete='family-name' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>

<FormField
control={form.control}
name='email'
render={({ field }) => (
<FormItem>
<FormLabel htmlFor='email'>Email</FormLabel>
<FormControl>
<Input placeholder='[email protected]' autoComplete='email' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name='password'
render={({ field }) => (
<FormItem>
<FormLabel htmlFor='password'>Password</FormLabel>
<FormControl>
<Input placeholder='********' type='password' autoComplete='current-password' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<Button type='submit' className='w-full' disabled={registerPending || loginPending}>
{registerPending || loginPending ? <LoadingSpinner className='' /> : 'Create account'}
</Button>
</form>
</Form>
</div>
);
};
Loading
Loading