Add Username to Onboarding, Fix Onboarding #28
@@ -2,9 +2,10 @@ import { NextResponse } from "next/server";
|
||||
// Lib Imports
|
||||
import { app } from "../firebase-config";
|
||||
import { getDatabase, ref, set as firebaseSet } from "firebase/database";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
|
||||
async function onboard(firstName, lastName, req) {
|
||||
async function onboard(onboardingJSON, req) {
|
||||
var session = req.cookies.get("session");
|
||||
//Call the authentication endpoint
|
||||
var res = await fetch(new URL("/api/login", req.url), {headers: {Cookie: `session=${session?.value}`}})
|
||||
@@ -14,16 +15,24 @@ async function onboard(firstName, lastName, req) {
|
||||
return NextResponse.json({}, { status: 401 });
|
||||
}
|
||||
try {
|
||||
var expiresIn = 20 * 60 * 1000; // 20 minutes
|
||||
var { uid, email } = await res.json()
|
||||
onboardingJSON.email = email
|
||||
onboardingJSON.uid = uid
|
||||
onboardingJSON.defined = true
|
||||
var database = getDatabase(app)
|
||||
await firebaseSet(ref(database, `users/${uid}`), {
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
email: email
|
||||
});
|
||||
await firebaseSet(ref(database, `users/${uid}`), onboardingJSON);
|
||||
var userOptions = {
|
||||
name: "user",
|
||||
value: JSON.stringify(onboardingJSON),
|
||||
maxAge: expiresIn, // 20 mins
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
};
|
||||
cookies().set(userOptions);
|
||||
return NextResponse.json({}, { status: 200 });
|
||||
} catch(error) {
|
||||
return NextResponse.json({ error: "Internal Server Error" },{ status: 500 });
|
||||
return NextResponse.json({ error: "Internal Server Error: "+error },{ status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +40,8 @@ async function onboard(firstName, lastName, req) {
|
||||
// Handles POST requests (login requests)
|
||||
export async function POST(req, res) {
|
||||
try {
|
||||
var { firstName, lastName } = await req?.json()
|
||||
return await onboard(firstName, lastName, req);
|
||||
var onboardingJSON = await req?.json()
|
||||
return await onboard(onboardingJSON, req);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Internal Server Error" },{ status: 500 });
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ function Onboarding() {
|
||||
Welcome to ChatMaps! We are excited to have you join our community!<br/>First we just need a little bit of information from you to get started.
|
||||
</div>
|
||||
<form action="#" onSubmit={handleSubmit(Onboard)}>
|
||||
<input type="text" {...register("username")} placeholder="Display Name"/><br/>
|
||||
<input type="text" {...register("firstName")} placeholder="First Name"/><br/>
|
||||
<input type="text" {...register("lastName")} placeholder="Last Name"/><br/>
|
||||
<button type="submit" className="bg-[#dee0e0] m-5">Save</button>
|
||||
|
||||
@@ -2,11 +2,29 @@
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useForm, Form } from "react-hook-form";
|
||||
import "../globals.css"
|
||||
import { useState } from "react";
|
||||
|
||||
function Register() {
|
||||
var { register, control, setError, formState: { errors, isSubmitting, isSubmitted } } = useForm()
|
||||
var { register, control, setError, handleSubmit, formState: { errors } } = useForm()
|
||||
var router = useRouter();
|
||||
var emailRegex = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/
|
||||
var [passwordMismatch, setPasswordMismatch] = useState(false);
|
||||
|
||||
const passwordMatch = (data) => {
|
||||
return data.password === data.passwordCheck;
|
||||
};
|
||||
|
||||
const onSubmit = (data) => {
|
||||
if (passwordMatch(data)) {
|
||||
setPasswordMismatch(false);
|
||||
router.push("/success");
|
||||
|
||||
} else{
|
||||
setPasswordMismatch(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="grid h-screen place-items-center">
|
||||
@@ -17,23 +35,20 @@ function Register() {
|
||||
</span>
|
||||
<div>
|
||||
<h3 className="text-[24px] mt-[15px]">Register</h3>
|
||||
<Form action="/api/register" encType={'application/json'}
|
||||
<Form onSubmit={handleSubmit(onSubmit)}
|
||||
onSuccess={() => {
|
||||
router.push("/app");
|
||||
}}
|
||||
action="/api/register"
|
||||
encType={'application/json'}
|
||||
control={control}
|
||||
>
|
||||
<input type="email" {...register("email", {required: true, pattern: emailRegex})} className={errors.email && "err"} placeholder="Enter Email Address"/><br/>
|
||||
<input type="password" {...register("password", {required: true})} className={errors.password && errors.password.type == 'required' && "err"} placeholder="Enter Password"/><br/>
|
||||
<button className="inline-flex items-center px-4 py-2 transition ease-in-out duration-150 bg-[#dee0e0] m-5 bg-cyan-500 text-white font-bold py-2 px-4 rounded-full">
|
||||
{(isSubmitting || isSubmitted) && <span className="inline-block">
|
||||
<svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
</span> }
|
||||
Register
|
||||
</button><br/>
|
||||
<input type ="password" {...register("passwordCheck", {required: false})} className ={errors.passwordCheckheck && errors.passwordCheck.type == 'required' && "err"} placeholder="Re-enter Password"/><br/>
|
||||
{passwordMismatch && <p className="text-red-500">Passwords do not match</p>}
|
||||
<button type="submit" className="bg-[#dee0e0] m-5 bg-cyan-500 text-white font-bold py-2 px-4 rounded-full">
|
||||
Register</button><br/>
|
||||
Have an account? <a href="/login">Log In</a>
|
||||
</Form>
|
||||
</div>
|
||||
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "ChatMaps",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {}
|
||||
}
|
||||
Reference in New Issue
Block a user