Updated the register to only allow to create users if they have a code available
This commit is contained in:
@@ -28,7 +28,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
const codePromises = codes.map(async (code, index) => {
|
||||
const codeRef = doc(db, "codes", uuidv4());
|
||||
const codeRef = doc(db, "codes", code);
|
||||
await setDoc(codeRef, {type, code});
|
||||
|
||||
if (emails && emails.length > index) {
|
||||
|
||||
@@ -3,8 +3,8 @@ import {createUserWithEmailAndPassword, getAuth, sendPasswordResetEmail} from "f
|
||||
import {app} from "@/firebase";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {getFirestore, getDoc, doc, setDoc} from "firebase/firestore";
|
||||
import {DemographicInformation} from "@/interfaces/user";
|
||||
import {getFirestore, getDoc, doc, setDoc, deleteDoc} from "firebase/firestore";
|
||||
import {DemographicInformation, Type} from "@/interfaces/user";
|
||||
|
||||
const auth = getAuth(app);
|
||||
const db = getFirestore(app);
|
||||
@@ -26,7 +26,15 @@ const DEFAULT_LEVELS = {
|
||||
};
|
||||
|
||||
async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, password} = req.body as {email: string; password: string; demographicInformation: DemographicInformation};
|
||||
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()) {
|
||||
res.status(400).json({error: "Invalid Code!"});
|
||||
return;
|
||||
}
|
||||
|
||||
const codeData = codeRef.data() as {code: string; type: Type};
|
||||
|
||||
createUserWithEmailAndPassword(auth, email, password)
|
||||
.then(async (userCredentials) => {
|
||||
@@ -38,12 +46,13 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||
levels: DEFAULT_LEVELS,
|
||||
bio: "",
|
||||
isFirstLogin: true,
|
||||
isFirstLogin: codeData.type === "student",
|
||||
focus: "academic",
|
||||
type: "student",
|
||||
type: codeData.type,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "users", userId), user);
|
||||
await deleteDoc(codeRef.ref);
|
||||
|
||||
req.session.user = {...user, id: userId};
|
||||
await req.session.save();
|
||||
|
||||
Reference in New Issue
Block a user