Add username, fix onboarding

This commit is contained in:
2024-02-24 02:15:53 +00:00
parent 7ca4b62848
commit 9ee8bf3376
3 changed files with 19 additions and 10 deletions
+18 -9
View File
@@ -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 });
}
+1
View File
@@ -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>
-1
View File
@@ -3,7 +3,6 @@ import { useRouter } from "next/navigation";
import { useForm, Form } from "react-hook-form";
import "../globals.css"
import { useState } from "react";
import { data } from "autoprefixer";
function Register() {
var { register, control, setError, handleSubmit, formState: { errors } } = useForm()