Updated the login and register to transform the e-mail to lowercase
This commit is contained in:
@@ -76,7 +76,7 @@ export default function BatchCodeGenerator({ user }: { user: User }) {
|
|||||||
] = row as string[];
|
] = row as string[];
|
||||||
return EMAIL_REGEX.test(email.toString().trim())
|
return EMAIL_REGEX.test(email.toString().trim())
|
||||||
? {
|
? {
|
||||||
email: email.toString().trim(),
|
email: email.toString().trim().toLowerCase(),
|
||||||
name: `${firstName ?? ""} ${lastName ?? ""}`.trim(),
|
name: `${firstName ?? ""} ${lastName ?? ""}`.trim(),
|
||||||
passport_id: passport_id?.toString().trim() || undefined,
|
passport_id: passport_id?.toString().trim() || undefined,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export default withIronSessionApiRoute(login, sessionOptions);
|
|||||||
async function login(req: NextApiRequest, res: NextApiResponse) {
|
async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const { email, password } = req.body as { email: string; password: string };
|
const { email, password } = req.body as { email: string; password: string };
|
||||||
|
|
||||||
signInWithEmailAndPassword(auth, email, password)
|
signInWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||||
.then(async (userCredentials) => {
|
.then(async (userCredentials) => {
|
||||||
const userId = userCredentials.user.uid;
|
const userId = userCredentials.user.uid;
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,20 @@ import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
|||||||
import { app } from "@/firebase";
|
import { app } from "@/firebase";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import {getFirestore, doc, setDoc, query, collection, where, getDocs} from "firebase/firestore";
|
import {
|
||||||
import {CorporateInformation, DemographicInformation, Type} from "@/interfaces/user";
|
getFirestore,
|
||||||
|
doc,
|
||||||
|
setDoc,
|
||||||
|
query,
|
||||||
|
collection,
|
||||||
|
where,
|
||||||
|
getDocs,
|
||||||
|
} from "firebase/firestore";
|
||||||
|
import {
|
||||||
|
CorporateInformation,
|
||||||
|
DemographicInformation,
|
||||||
|
Type,
|
||||||
|
} from "@/interfaces/user";
|
||||||
import { addUserToGroupOnCreation } from "@/utils/registration";
|
import { addUserToGroupOnCreation } from "@/utils/registration";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
|
|
||||||
@@ -45,29 +57,46 @@ async function registerIndividual(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const codeQuery = query(collection(db, "codes"), where("code", "==", code));
|
const codeQuery = query(collection(db, "codes"), where("code", "==", code));
|
||||||
const codeDocs = (await getDocs(codeQuery)).docs.filter((x) => !Object.keys(x.data()).includes("userId"));
|
const codeDocs = (await getDocs(codeQuery)).docs.filter(
|
||||||
|
(x) => !Object.keys(x.data()).includes("userId"),
|
||||||
|
);
|
||||||
|
|
||||||
if (code && code.length > 0 && codeDocs.length === 0) {
|
if (code && code.length > 0 && codeDocs.length === 0) {
|
||||||
res.status(400).json({ error: "Invalid Code!" });
|
res.status(400).json({ error: "Invalid Code!" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const codeData = codeDocs.length > 0 ? (codeDocs[0].data() as {code: string; type: Type; creator?: string; expiryDate: Date | null}) : undefined;
|
const codeData =
|
||||||
|
codeDocs.length > 0
|
||||||
|
? (codeDocs[0].data() as {
|
||||||
|
code: string;
|
||||||
|
type: Type;
|
||||||
|
creator?: string;
|
||||||
|
expiryDate: Date | null;
|
||||||
|
})
|
||||||
|
: undefined;
|
||||||
|
|
||||||
createUserWithEmailAndPassword(auth, email, password)
|
createUserWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||||
.then(async (userCredentials) => {
|
.then(async (userCredentials) => {
|
||||||
const userId = userCredentials.user.uid;
|
const userId = userCredentials.user.uid;
|
||||||
delete req.body.password;
|
delete req.body.password;
|
||||||
|
|
||||||
const user = {
|
const user = {
|
||||||
...req.body,
|
...req.body,
|
||||||
|
email: email.toLowerCase(),
|
||||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||||
levels: DEFAULT_LEVELS,
|
levels: DEFAULT_LEVELS,
|
||||||
bio: "",
|
bio: "",
|
||||||
isFirstLogin: codeData ? codeData.type === "student" : true,
|
isFirstLogin: codeData ? codeData.type === "student" : true,
|
||||||
focus: "academic",
|
focus: "academic",
|
||||||
type: email.endsWith("@ecrop.dev") ? "developer" : codeData ? codeData.type : "student",
|
type: email.endsWith("@ecrop.dev")
|
||||||
subscriptionExpirationDate: codeData ? codeData.expiryDate : moment().subtract(1, "days").toISOString(),
|
? "developer"
|
||||||
|
: codeData
|
||||||
|
? codeData.type
|
||||||
|
: "student",
|
||||||
|
subscriptionExpirationDate: codeData
|
||||||
|
? codeData.expiryDate
|
||||||
|
: moment().subtract(1, "days").toISOString(),
|
||||||
...(passport_id ? { demographicInformation: { passport_id } } : {}),
|
...(passport_id ? { demographicInformation: { passport_id } } : {}),
|
||||||
registrationDate: new Date().toISOString(),
|
registrationDate: new Date().toISOString(),
|
||||||
status: code ? "active" : "paymentDue",
|
status: code ? "active" : "paymentDue",
|
||||||
@@ -77,7 +106,12 @@ async function registerIndividual(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
if (codeDocs.length > 0 && codeData) {
|
if (codeDocs.length > 0 && codeData) {
|
||||||
await setDoc(codeDocs[0].ref, { userId: userId }, { merge: true });
|
await setDoc(codeDocs[0].ref, { userId: userId }, { merge: true });
|
||||||
if (codeData.creator) await addUserToGroupOnCreation(userId, codeData.type, codeData.creator);
|
if (codeData.creator)
|
||||||
|
await addUserToGroupOnCreation(
|
||||||
|
userId,
|
||||||
|
codeData.type,
|
||||||
|
codeData.creator,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
req.session.user = { ...user, id: userId };
|
req.session.user = { ...user, id: userId };
|
||||||
@@ -98,13 +132,14 @@ async function registerCorporate(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
corporateInformation: CorporateInformation;
|
corporateInformation: CorporateInformation;
|
||||||
};
|
};
|
||||||
|
|
||||||
createUserWithEmailAndPassword(auth, email, password)
|
createUserWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||||
.then(async (userCredentials) => {
|
.then(async (userCredentials) => {
|
||||||
const userId = userCredentials.user.uid;
|
const userId = userCredentials.user.uid;
|
||||||
delete req.body.password;
|
delete req.body.password;
|
||||||
|
|
||||||
const user = {
|
const user = {
|
||||||
...req.body,
|
...req.body,
|
||||||
|
email: email.toLowerCase(),
|
||||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||||
levels: DEFAULT_LEVELS,
|
levels: DEFAULT_LEVELS,
|
||||||
bio: "",
|
bio: "",
|
||||||
|
|||||||
Reference in New Issue
Block a user