Skip to content

Commit

Permalink
feat: setup entire login
Browse files Browse the repository at this point in the history
  • Loading branch information
devsargam committed Mar 10, 2024
1 parent 2937904 commit 735b683
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@auth/prisma-adapter": "^1.0.6",
"@discordjs/core": "^1.1.1",
"@discordjs/next": "^0.1.1-dev.1673526225-a580768.0",
"@hookform/resolvers": "^3.3.4",
"@prisma/client": "^5.6.0",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-avatar": "^1.0.4",
Expand All @@ -45,7 +46,7 @@
"notion-client": "^6.16.0",
"react": "^18",
"react-dom": "^18",
"react-hook-form": "^7.50.1",
"react-hook-form": "^7.51.0",
"react-notion-x": "^6.16.0",
"react-resizable-panels": "^1.0.7",
"recoil": "^0.7.7",
Expand Down
13 changes: 12 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "password" TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "isVerified" BOOLEAN NOT NULL DEFAULT false;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ model User {
id String @id @default(cuid())
name String?
email String? @unique
password String?
isVerified Boolean @default(false)
token String?
sessions Session[]
purchases UserPurchases[]
Expand Down
35 changes: 35 additions & 0 deletions src/app/api/auth/register/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NextResponse } from 'next/server';
import prisma from '@/db';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';

export async function POST(request: Request) {
try {
const { name, email, password } = await request.json();

// TODO: Verify User
// TODO: Hash password
await prisma.user.create({
data: {
email,
name,
password,
},
});

return NextResponse.json(
{ message: 'Account created sucessfully' },
{ status: 201 },
);
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
return NextResponse.json(
{ error: 'Email already taken.' },
{ status: 400 },
);
}
return NextResponse.json(
{ error: 'Failed to parse JSON input' },
{ status: 400 },
);
}
}
16 changes: 16 additions & 0 deletions src/app/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SignupForm } from '@/components/Signup';
import { authOptions } from '@/lib/auth';
import { getServerSession } from 'next-auth';
import { redirect } from 'next/navigation';
import React from 'react';

const SigninPage = async () => {
const session = await getServerSession(authOptions);
if (session?.user) {
redirect('/');
}

return <SignupForm />;
};

export default SigninPage;
5 changes: 1 addition & 4 deletions src/components/Appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ export const Appbar = () => {
<AppbarAuth />

<Button size={'sm'} asChild>
<Link
href={'https://harkirat.classx.co.in/new-courses'}
target="_blank"
>
<Link href={'/signup'}>
<p className="text-white">Join now</p>{' '}
<Sparkles className="text-white ml-2 h-4 w-4 hover:translate-x-0.5 ease-linear duration-200" />
</Link>
Expand Down
109 changes: 109 additions & 0 deletions src/components/Signup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use client';
import { zodResolver } from '@hookform/resolvers/zod';
import React from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { toast } from 'sonner';

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

const signupFormSchema = z.object({
fullName: z.string().min(2).max(50),
email: z.string().email(),
password: z.string().min(8).max(20),
});

export function SignupForm() {
const form = useForm<z.infer<typeof signupFormSchema>>({
resolver: zodResolver(signupFormSchema),
defaultValues: {
fullName: '',
email: '',
password: '',
},
});

async function onSubmit(values: z.infer<typeof signupFormSchema>) {
const res = await fetch('/api/auth/register', {
method: 'POST',
body: JSON.stringify({ ...values }),
});

const data = await res.json();
console.log(data);

if (res.status === 201) {
toast.info(data.message, {
description: 'Go to login to access the site',
});
} else {
console.log('here');
toast.error(data.error, {
description: 'Something went wrong. Uh oh!',
});
}
}

return (
<>
<main className="flex justify-center w-full mt-10 px-5">
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-8 max-w-lg w-full"
>
<FormField
control={form.control}
name="fullName"
render={({ field }) => (
<FormItem>
<FormLabel>Full Name</FormLabel>
<FormControl>
<Input placeholder="sargam" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input placeholder="[email protected]" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input type="password" placeholder="password" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
</main>
</>
);
}
5 changes: 1 addition & 4 deletions src/components/landing/appbar/nav-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ export function NavigationMenu() {
<div className="flex flex-col items-center justify-center space-y-2">
<Button size={'lg'} variant={'navLink'} asChild>
<DrawerClose>
<Link
href={'https://harkirat.classx.co.in/new-courses'}
target="_blank"
>
<Link href={'/signup'}>
<p className="text-zinc-950 dark:text-white">Join now</p>
</Link>
</DrawerClose>
Expand Down
5 changes: 1 addition & 4 deletions src/components/landing/footer/pre-footer/pre-footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ const PreFooterSection = () => {
</div>

<Button size={'lg'} className="text-lg rounded-full" asChild>
<Link
href={'https://harkirat.classx.co.in/new-courses'}
target="_blank"
>
<Link href={'/signup'}>
<p className="text-white">Join now</p>{' '}
<Sparkles className="text-white ml-2 h-4 w-4 hover:translate-x-0.5 ease-linear duration-200" />
</Link>
Expand Down
Loading

0 comments on commit 735b683

Please sign in to comment.