Polishing Touches, Remove of Unnecessary Files, Register -> Onboard
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
import { initializeApp, getApps } from "firebase-admin/app";
|
||||
import admin from "firebase-admin";
|
||||
|
||||
export function customInitApp() {
|
||||
if (getApps().length <= 0) {
|
||||
initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
type: process.env.FIREBASE_ADMIN_TYPE,
|
||||
projectId: process.env.FIREBASE_ADMIN_PROJECT_ID,
|
||||
privateKeyId: process.env.FIREBASE_ADMIN_PRIV_KEY_ID,
|
||||
privateKey: process.env.FIREBASE_ADMIN_PRIV_KEY?.replace(/\\n/g, "\n"),
|
||||
clientEmail: process.env.FIREBASE_ADMIN_CLIENT_EMAIL,
|
||||
clientId: process.env.FIREBASE_ADMIN_CLIENT_ID,
|
||||
authUri: process.env.FIREBASE_ADMIN_AUTH_URI,
|
||||
tokenUri: process.env.FIREBASE_ADMIN_TOKEN_URL,
|
||||
authProviderX509CertUrl: process.env.FIREBASE_ADMIN_AUTH_PROVIDER_X509_CERT_URL,
|
||||
clientC509CertUrl: process.env.FIREBASE_ADMIN_CLIENT_X509_CERT_URL,
|
||||
universe_domain: process.env.FIREBASE_ADMIN_UNIVERSE_DOMAIN,
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
import { NextResponse } from "next/server";
|
||||
// Lib Imports
|
||||
import { database } from "../firebase-config";
|
||||
import { ref, set as firebaseSet } from "firebase/database";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
|
||||
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}`}})
|
||||
|
||||
// Login if unauthorized
|
||||
if (res.status !== 200) {
|
||||
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
|
||||
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: "+error },{ status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Handles POST requests (login requests)
|
||||
export async function POST(req, res) {
|
||||
try {
|
||||
var onboardingJSON = await req?.json()
|
||||
return await onboard(onboardingJSON, req);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Internal Server Error" },{ status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import {signOut} from "firebase/auth";
|
||||
import {auth} from "../firebase-config";
|
||||
|
||||
|
||||
export async function GET(req) {
|
||||
cookies().delete('user')
|
||||
cookies().delete('session')
|
||||
cookies().delete('uid')
|
||||
await signOut(auth)
|
||||
return NextResponse.redirect(new URL("/",req.url))
|
||||
}
|
||||
@@ -30,7 +30,7 @@ function Login() {
|
||||
async function authenticationPush({data}) {
|
||||
if (authenticate(data)) {
|
||||
console.log("Fire")
|
||||
router.push("/");
|
||||
router.push("/app");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,22 +2,35 @@
|
||||
import "../globals.css"
|
||||
import { useForm } from "react-hook-form";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { ref, set } from "firebase/database";
|
||||
import {auth, database} from "../api/firebase-config"
|
||||
import {onAuthStateChanged} from "firebase/auth"
|
||||
|
||||
function createUser(data) {
|
||||
onAuthStateChanged(auth, (user) => {
|
||||
if (user.uid) {
|
||||
console.log(user)
|
||||
data.uid = user.uid
|
||||
data.defined = true
|
||||
data.email = user.email
|
||||
set(ref(database, `users/${user.uid}`), data);
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
function Onboarding() {
|
||||
var router = useRouter();
|
||||
var { register, handleSubmit } = useForm();
|
||||
|
||||
async function Onboard(data) {
|
||||
const res = await fetch("/api/onboard", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data ? data : {}),
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
router.push("/app");
|
||||
} else {
|
||||
router.push("/login");
|
||||
}
|
||||
function Onboard(data) {
|
||||
createUser(data)
|
||||
router.push("/app");
|
||||
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
|
||||
@@ -4,13 +4,21 @@ import { useForm, Form } from "react-hook-form";
|
||||
import "../globals.css"
|
||||
import { useState } from "react";
|
||||
|
||||
import { createUserWithEmailAndPassword } from "firebase/auth";
|
||||
import { createUserWithEmailAndPassword, signInWithEmailAndPassword, setPersistence, browserSessionPersistence } from "firebase/auth";
|
||||
import {auth} from "../api/firebase-config";
|
||||
|
||||
async function Signup(data) {
|
||||
var userCredential = await createUserWithEmailAndPassword(auth,data.email,data.password);
|
||||
if (userCredential.user) {
|
||||
return true
|
||||
setPersistence(auth, browserSessionPersistence)
|
||||
.then(() => {
|
||||
signInWithEmailAndPassword(auth,data.email,data.password)
|
||||
.then((res) => {
|
||||
console.log(res)
|
||||
return true
|
||||
})
|
||||
})
|
||||
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
@@ -29,7 +37,7 @@ function Register() {
|
||||
if (passwordMatch(data)) {
|
||||
setPasswordMismatch(false);
|
||||
if (Signup(data)) {
|
||||
router.push("/app");
|
||||
router.push("/onboarding");
|
||||
}
|
||||
|
||||
} else{
|
||||
|
||||
@@ -2,6 +2,11 @@ import { auth, database } from "../../app/api/firebase-config";
|
||||
import { ref, set, remove } from "firebase/database";
|
||||
import {signOut} from "firebase/auth";
|
||||
|
||||
function logout() {
|
||||
console.log("Fire")
|
||||
signOut(auth)
|
||||
}
|
||||
|
||||
// Closes chat room
|
||||
function closeChatRoom(roomObj, setChatRoomObj, setMainTab, user) {
|
||||
var path = roomObj.path + "/" + roomObj.name + "-" + roomObj.timestamp;
|
||||
@@ -110,7 +115,8 @@ export function Header({mainTab, isMyRoom, chatRoomObj, setChatRoomObj, setMainT
|
||||
</a>
|
||||
)}
|
||||
<a
|
||||
href={signOut}
|
||||
onClick={logout}
|
||||
href="/"
|
||||
className="p-2 cursor-pointer bg-[#dee0e0] bg-cyan-500 text-white font-bold rounded-full"
|
||||
>
|
||||
Sign Out
|
||||
|
||||
Reference in New Issue
Block a user