Updated a bit more of the way the codes work
This commit is contained in:
@@ -29,7 +29,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const codePromises = codes.map(async (code, index) => {
|
||||
const codeRef = doc(db, "codes", code);
|
||||
await setDoc(codeRef, {type, code});
|
||||
await setDoc(codeRef, {type, code, creator: req.session.user!.id});
|
||||
|
||||
if (emails && emails.length > index) {
|
||||
const transport = prepareMailer();
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import {NextApiRequest, NextApiResponse} from "next";
|
||||
import {createUserWithEmailAndPassword, getAuth, sendPasswordResetEmail} from "firebase/auth";
|
||||
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
||||
import {app} from "@/firebase";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {getFirestore, getDoc, doc, setDoc, deleteDoc} from "firebase/firestore";
|
||||
import {getFirestore, doc, setDoc, query, collection, where, getDocs} from "firebase/firestore";
|
||||
import {DemographicInformation, Type} from "@/interfaces/user";
|
||||
|
||||
const auth = getAuth(app);
|
||||
@@ -28,13 +28,15 @@ const DEFAULT_LEVELS = {
|
||||
async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, password, code} = req.body as {email: string; password: string; code: string; demographicInformation: DemographicInformation};
|
||||
|
||||
const codeRef = await getDoc(doc(db, "codes", code));
|
||||
if (!codeRef.exists()) {
|
||||
const codeQuery = query(collection(db, "codes"), where("code", "==", code));
|
||||
const codeDocs = (await getDocs(codeQuery)).docs.filter((x) => !Object.keys(x.data()).includes("userId"));
|
||||
|
||||
if (codeDocs.length === 0) {
|
||||
res.status(400).json({error: "Invalid Code!"});
|
||||
return;
|
||||
}
|
||||
|
||||
const codeData = codeRef.data() as {code: string; type: Type};
|
||||
const codeData = codeDocs[0].data() as {code: string; type: Type};
|
||||
|
||||
createUserWithEmailAndPassword(auth, email, password)
|
||||
.then(async (userCredentials) => {
|
||||
@@ -52,7 +54,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "users", userId), user);
|
||||
await deleteDoc(codeRef.ref);
|
||||
await setDoc(codeDocs[0].ref, {userId: userId}, {merge: true});
|
||||
|
||||
req.session.user = {...user, id: userId};
|
||||
await req.session.save();
|
||||
|
||||
Reference in New Issue
Block a user