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[];
|
||||
return EMAIL_REGEX.test(email.toString().trim())
|
||||
? {
|
||||
email: email.toString().trim(),
|
||||
email: email.toString().trim().toLowerCase(),
|
||||
name: `${firstName ?? ""} ${lastName ?? ""}`.trim(),
|
||||
passport_id: passport_id?.toString().trim() || undefined,
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ export default withIronSessionApiRoute(login, sessionOptions);
|
||||
async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { email, password } = req.body as { email: string; password: string };
|
||||
|
||||
signInWithEmailAndPassword(auth, email, password)
|
||||
signInWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||
.then(async (userCredentials) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
|
||||
|
||||
@@ -3,8 +3,20 @@ import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
||||
import { app } from "@/firebase";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import {getFirestore, doc, setDoc, query, collection, where, getDocs} from "firebase/firestore";
|
||||
import {CorporateInformation, DemographicInformation, Type} from "@/interfaces/user";
|
||||
import {
|
||||
getFirestore,
|
||||
doc,
|
||||
setDoc,
|
||||
query,
|
||||
collection,
|
||||
where,
|
||||
getDocs,
|
||||
} from "firebase/firestore";
|
||||
import {
|
||||
CorporateInformation,
|
||||
DemographicInformation,
|
||||
Type,
|
||||
} from "@/interfaces/user";
|
||||
import { addUserToGroupOnCreation } from "@/utils/registration";
|
||||
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 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) {
|
||||
res.status(400).json({ error: "Invalid Code!" });
|
||||
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) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
delete req.body.password;
|
||||
|
||||
const user = {
|
||||
...req.body,
|
||||
email: email.toLowerCase(),
|
||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||
levels: DEFAULT_LEVELS,
|
||||
bio: "",
|
||||
isFirstLogin: codeData ? codeData.type === "student" : true,
|
||||
focus: "academic",
|
||||
type: email.endsWith("@ecrop.dev") ? "developer" : codeData ? codeData.type : "student",
|
||||
subscriptionExpirationDate: codeData ? codeData.expiryDate : moment().subtract(1, "days").toISOString(),
|
||||
type: email.endsWith("@ecrop.dev")
|
||||
? "developer"
|
||||
: codeData
|
||||
? codeData.type
|
||||
: "student",
|
||||
subscriptionExpirationDate: codeData
|
||||
? codeData.expiryDate
|
||||
: moment().subtract(1, "days").toISOString(),
|
||||
...(passport_id ? { demographicInformation: { passport_id } } : {}),
|
||||
registrationDate: new Date().toISOString(),
|
||||
status: code ? "active" : "paymentDue",
|
||||
@@ -77,7 +106,12 @@ async function registerIndividual(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
if (codeDocs.length > 0 && codeData) {
|
||||
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 };
|
||||
@@ -98,13 +132,14 @@ async function registerCorporate(req: NextApiRequest, res: NextApiResponse) {
|
||||
corporateInformation: CorporateInformation;
|
||||
};
|
||||
|
||||
createUserWithEmailAndPassword(auth, email, password)
|
||||
createUserWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||
.then(async (userCredentials) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
delete req.body.password;
|
||||
|
||||
const user = {
|
||||
...req.body,
|
||||
email: email.toLowerCase(),
|
||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||
levels: DEFAULT_LEVELS,
|
||||
bio: "",
|
||||
|
||||
Reference in New Issue
Block a user