diff --git a/app/lib/actions.ts b/app/lib/actions.ts index f9d8368..6f4c9b1 100644 --- a/app/lib/actions.ts +++ b/app/lib/actions.ts @@ -1,8 +1,6 @@ 'use server'; import { z } from 'zod'; -import { revalidatePath } from 'next/cache'; -import { redirect } from 'next/navigation'; import { signIn } from '@/auth'; import { AuthError } from 'next-auth'; @@ -23,30 +21,20 @@ const FormSchema = z.object({ }); -export async function authenticate( - prevState: string | undefined, - formData: FormData, -) { +export async function authenticate(email:string, password:string) { try { console.log("FORM"); const session = await auth(); console.log(session); - await signIn('credentials', formData); + await signIn('credentials', {email:email, password:password}); } catch (error) { - if (error instanceof AuthError) { - switch (error.type) { - case 'CredentialsSignin': - return 'Invalid credentials.'; - default: - return 'Something went wrong.'; - } - } + console.log("ERROR"); + console.log(error); + return "Invalid Login" } } export async function authUser(email: string, password:string){ - console.log(email); - console.log(password); let resp = { error: "", response: "", @@ -80,3 +68,118 @@ export async function authUser(email: string, password:string){ } + +export async function SignUp(firstname:string, lastname:string, email:string, password:string, confirmpassword:string) { + let resp = { + error: "", + response: "", + }; + console.log(firstname); + console.log(lastname); + console.log(email); + console.log(password); + console.log(confirmpassword); + if (!firstname) { + resp.error = "Invalid first name"; + return resp; + } else if (!lastname) { + resp.error = "Invalid last name"; + return resp; + } else if (!email) { + resp.error = "Invalid email"; + return resp; + } else if (!password) { + resp.error = "Invalid password"; + return resp; + } else if (!confirmpassword) { + resp.error = "Invalid password"; + return resp; + } else if (password !== confirmpassword) { + resp.error = "Passwords don't match"; + return resp; + } else { + + await fetch("https://api.hackru.org/dev/create", { + method: "POST", + headers: { + "content-type": "application/json", + }, + body: JSON.stringify({ + email: email, + password: password, + registration_status: "unregistered", //"waitlist" is one of them + }), + }) + .then(async (res) => { + let res_json = await res.json(); + if (res_json.statusCode === 400) { + resp.error = + "User with email " + email + " already exists"; + } else if (res_json.statusCode === 200) { + // Set the first and last name + let data = res_json.body; + let token = data.token; + + await fetch("https://api.hackru.org/dev/update", { + method: "POST", + headers: { + "content-type": "application/json", + }, + body: JSON.stringify({ + updates: { + $set: { + first_name: firstname, + last_name: lastname, + }, + }, + user_email: email, + auth_email: email, + token: token, + }), + }) + .then(async (res) => { + let res_json = await res.json(); + if (res_json.statusCode === 200) { + try { + await signIn('credentials', {email:email, password:password}); + } catch (error) { + if (error instanceof AuthError) { + switch (error.type) { + case 'CredentialsSignin': + resp.error = 'Invalid credentials.'; + default: + resp.error = 'Something went wrong.'; + } + } + } + } else { + if (res_json.body) { + resp.error = res_json.body; + } else { + resp.error = "Unexpected Error"; + } + } + }) + .catch((error) => { + resp.error = + error + + "; An error occured when attempting signup. Failed at 2/2"; + }); + } else { + if (res_json.body) { + resp.error = res_json.body; + } else { + resp.error = "Unexpected Error"; + } + } + }) + .catch((error) => { + resp.error = + error + + "; An error occured when attempting signup. Failed at 1/2"; + }); + } + + return resp; +} + diff --git a/app/login/page.tsx b/app/login/page.tsx index cbe006a..d53e940 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -2,19 +2,39 @@ import { Button } from '@/app/ui/button'; -import { useFormState } from 'react-dom'; + import { authenticate, authUser } from '../lib/actions'; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { z } from 'zod'; + export default function LoginPage() { - const [errorMessage, dispatch] = useFormState(authenticate, undefined); + + const LoginSchema = z.object({ + email: z.string().email(), + password: z.string(), + }); + + type Login = z.infer; + + + + const {register,handleSubmit,reset, formState: { errors },} = useForm({resolver: zodResolver(LoginSchema)}); + + const onSubmit = (data: Login) => { + console.log("Hi"); + authenticate(data.email, data.password); + } + return (
-
+
diff --git a/app/signup/page.tsx b/app/signup/page.tsx new file mode 100644 index 0000000..b002eef --- /dev/null +++ b/app/signup/page.tsx @@ -0,0 +1,151 @@ +"use client" + +import { Button } from '@/app/ui/button'; + + +import { SignUp } from '../lib/actions'; + +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { z } from 'zod'; + +export default function SignupPage() { + + const SignUpSchema = z.object({ + email: z.string().email(), + + first_name: z.string(), + last_name: z.string(), + + password: z.string(), + confirm_password: z.string() + + }).refine((data) => data.password === data.confirm_password, { + message: "Passwords don't match", + path: ["confirm_password"], // path of error + }); + + type SignUp = z.infer; + + + + const {register,handleSubmit,reset, formState: { errors },} = useForm({resolver: zodResolver(SignUpSchema)}); + + const onSubmit = (data: SignUp) => { + console.log("SENDING SIGNUP"); + console.log(data); + const resp = SignUp(data.first_name, data.last_name, data.email, data.password, data.confirm_password); + console.log(resp); + } + + + return ( +
+ +
+
+ +
+ + {errors.email && (

{errors.email?.message}

)} +
+
+ +
+ +
+ + {errors.first_name && (

{errors.first_name?.message}

)} +
+
+ +
+ +
+ + {errors.last_name && (

{errors.last_name?.message}

)} +
+
+ +
+ +
+ + {errors.password && (

{errors.password?.message}

)} +
+
+
+ +
+ + {errors.confirm_password && (

{errors.confirm_password?.message}

)} +
+
+ +
+ + +
+ ); +}