-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
359 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20240310064022_add_password_field/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "password" TEXT; |
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20240310064345_add_is_verified_field/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.