New feature on the account creation:

It automatically stores who created the code and adds the registered user to a group administrated by that creator
This commit is contained in:
Tiago Ribeiro
2023-10-10 23:00:36 +01:00
parent 1aa4f0ddfd
commit 634a396434
6 changed files with 55 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ import {sessionOptions} from "@/lib/session";
import {withIronSessionApiRoute} from "iron-session/next";
import {getFirestore, doc, setDoc, query, collection, where, getDocs} from "firebase/firestore";
import {DemographicInformation, Type} from "@/interfaces/user";
import {addUserToGroupOnCreation} from "@/utils/registration";
const auth = getAuth(app);
const db = getFirestore(app);
@@ -36,7 +37,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
return;
}
const codeData = codeDocs[0].data() as {code: string; type: Type};
const codeData = codeDocs[0].data() as {code: string; type: Type; creator: string};
createUserWithEmailAndPassword(auth, email, password)
.then(async (userCredentials) => {
@@ -55,6 +56,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
await setDoc(doc(db, "users", userId), user);
await setDoc(codeDocs[0].ref, {userId: userId}, {merge: true});
await addUserToGroupOnCreation(userId, codeData.type, codeData.creator);
req.session.user = {...user, id: userId};
await req.session.save();