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,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import {NextApiRequest, NextApiResponse} from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import {getAuth, signInWithEmailAndPassword} from "firebase/auth";
|
import { getAuth, signInWithEmailAndPassword } 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 {User} from "@/interfaces/user";
|
import { User } from "@/interfaces/user";
|
||||||
import {getFirestore, getDoc, doc} from "firebase/firestore";
|
import { getFirestore, getDoc, doc } from "firebase/firestore";
|
||||||
|
|
||||||
const auth = getAuth(app);
|
const auth = getAuth(app);
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
@@ -12,27 +12,27 @@ const db = getFirestore(app);
|
|||||||
export default withIronSessionApiRoute(login, sessionOptions);
|
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;
|
||||||
|
|
||||||
const docUser = await getDoc(doc(db, "users", userId));
|
const docUser = await getDoc(doc(db, "users", userId));
|
||||||
if (!docUser.exists()) {
|
if (!docUser.exists()) {
|
||||||
res.status(401).json({error: 401, message: "User does not exist!"});
|
res.status(401).json({ error: 401, message: "User does not exist!" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = docUser.data() as User;
|
const user = docUser.data() as User;
|
||||||
|
|
||||||
req.session.user = {...user, id: userId};
|
req.session.user = { ...user, id: userId };
|
||||||
await req.session.save();
|
await req.session.save();
|
||||||
|
|
||||||
res.status(200).json({user: {...user, id: userId}});
|
res.status(200).json({ user: { ...user, id: userId } });
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(401).json({error});
|
res.status(401).json({ error });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,23 @@
|
|||||||
import {NextApiRequest, NextApiResponse} from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
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,
|
||||||
import {addUserToGroupOnCreation} from "@/utils/registration";
|
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";
|
import moment from "moment";
|
||||||
|
|
||||||
const auth = getAuth(app);
|
const auth = getAuth(app);
|
||||||
@@ -14,117 +26,140 @@ const db = getFirestore(app);
|
|||||||
export default withIronSessionApiRoute(register, sessionOptions);
|
export default withIronSessionApiRoute(register, sessionOptions);
|
||||||
|
|
||||||
const DEFAULT_DESIRED_LEVELS = {
|
const DEFAULT_DESIRED_LEVELS = {
|
||||||
reading: 9,
|
reading: 9,
|
||||||
listening: 9,
|
listening: 9,
|
||||||
writing: 9,
|
writing: 9,
|
||||||
speaking: 9,
|
speaking: 9,
|
||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_LEVELS = {
|
const DEFAULT_LEVELS = {
|
||||||
reading: 0,
|
reading: 0,
|
||||||
listening: 0,
|
listening: 0,
|
||||||
writing: 0,
|
writing: 0,
|
||||||
speaking: 0,
|
speaking: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
async function register(req: NextApiRequest, res: NextApiResponse) {
|
async function register(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const {type} = req.body as {
|
const { type } = req.body as {
|
||||||
type: "individual" | "corporate";
|
type: "individual" | "corporate";
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type === "individual") return registerIndividual(req, res);
|
if (type === "individual") return registerIndividual(req, res);
|
||||||
if (type === "corporate") return registerCorporate(req, res);
|
if (type === "corporate") return registerCorporate(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function registerIndividual(req: NextApiRequest, res: NextApiResponse) {
|
async function registerIndividual(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const {email, passport_id, password, code} = req.body as {
|
const { email, passport_id, password, code } = req.body as {
|
||||||
email: string;
|
email: string;
|
||||||
passport_id?: string;
|
passport_id?: string;
|
||||||
password: string;
|
password: string;
|
||||||
code?: string;
|
code?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
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,
|
||||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
email: email.toLowerCase(),
|
||||||
levels: DEFAULT_LEVELS,
|
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||||
bio: "",
|
levels: DEFAULT_LEVELS,
|
||||||
isFirstLogin: codeData ? codeData.type === "student" : true,
|
bio: "",
|
||||||
focus: "academic",
|
isFirstLogin: codeData ? codeData.type === "student" : true,
|
||||||
type: email.endsWith("@ecrop.dev") ? "developer" : codeData ? codeData.type : "student",
|
focus: "academic",
|
||||||
subscriptionExpirationDate: codeData ? codeData.expiryDate : moment().subtract(1, "days").toISOString(),
|
type: email.endsWith("@ecrop.dev")
|
||||||
...(passport_id ? {demographicInformation: {passport_id}} : {}),
|
? "developer"
|
||||||
registrationDate: new Date().toISOString(),
|
: codeData
|
||||||
status: code ? "active" : "paymentDue",
|
? 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",
|
||||||
|
};
|
||||||
|
|
||||||
await setDoc(doc(db, "users", userId), user);
|
await setDoc(doc(db, "users", userId), user);
|
||||||
|
|
||||||
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 };
|
||||||
await req.session.save();
|
await req.session.save();
|
||||||
|
|
||||||
res.status(200).json({user: {...user, id: userId}});
|
res.status(200).json({ user: { ...user, id: userId } });
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(401).json({error});
|
res.status(401).json({ error });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function registerCorporate(req: NextApiRequest, res: NextApiResponse) {
|
async function registerCorporate(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const {email, password} = req.body as {
|
const { email, password } = req.body as {
|
||||||
email: string;
|
email: string;
|
||||||
password: string;
|
password: string;
|
||||||
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,
|
||||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
email: email.toLowerCase(),
|
||||||
levels: DEFAULT_LEVELS,
|
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||||
bio: "",
|
levels: DEFAULT_LEVELS,
|
||||||
isFirstLogin: false,
|
bio: "",
|
||||||
focus: "academic",
|
isFirstLogin: false,
|
||||||
type: "corporate",
|
focus: "academic",
|
||||||
subscriptionExpirationDate: req.body.subscriptionExpirationDate || null,
|
type: "corporate",
|
||||||
status: "paymentDue",
|
subscriptionExpirationDate: req.body.subscriptionExpirationDate || null,
|
||||||
registrationDate: new Date().toISOString(),
|
status: "paymentDue",
|
||||||
};
|
registrationDate: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
|
||||||
await setDoc(doc(db, "users", userId), user);
|
await setDoc(doc(db, "users", userId), user);
|
||||||
|
|
||||||
req.session.user = {...user, id: userId};
|
req.session.user = { ...user, id: userId };
|
||||||
await req.session.save();
|
await req.session.save();
|
||||||
|
|
||||||
res.status(200).json({user: {...user, id: userId}});
|
res.status(200).json({ user: { ...user, id: userId } });
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(401).json({error});
|
res.status(401).json({ error });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user