require invite token to register except for initial registration

This commit is contained in:
Alex Holliday committed 2024-09-25 15:12:07 +08:00
1 parent 4e8675df45
commit 5dbd8378e2
6 files changed
+42 -3

No files matched your search

+3 -2
View File
@@ -596,6 +596,7 @@ const Register = ({ isSuperAdmin }) => {
let registerForm = { let registerForm = {
...form, ...form,
role: isSuperAdmin ? ["superadmin"] : form.role, role: isSuperAdmin ? ["superadmin"] : form.role,
inviteToken: token ? token : "",
}; };
let error = validateForm(registerForm, { let error = validateForm(registerForm, {
context: { password: form.password }, context: { password: form.password },
@@ -608,8 +609,8 @@ const Register = ({ isSuperAdmin }) => {
delete registerForm.confirm; delete registerForm.confirm;
const action = await dispatch(register(registerForm)); const action = await dispatch(register(registerForm));
if (action.payload.success) { if (action.payload.success) {
const token = action.payload.data; const authToken = action.payload.data;
localStorage.setItem("token", token); localStorage.setItem("token", authToken);
navigate("/"); navigate("/");
createToast({ createToast({
body: "Welcome! Your account was created successfully.", body: "Welcome! Your account was created successfully.",
+1
View File
@@ -77,6 +77,7 @@ const credentials = joi.object({
}), }),
role: joi.array(), role: joi.array(),
teamId: joi.string().allow("").optional(), teamId: joi.string().allow("").optional(),
inviteToken: joi.string().allow(""),
}); });
const monitorValidation = joi.object({ const monitorValidation = joi.object({
+5
View File
@@ -51,6 +51,11 @@ const registerController = async (req, res, next) => {
} }
// Create a new user // Create a new user
try { try {
const { inviteToken } = req.body;
const superAdminExists = await req.db.checkSuperadmin(req, res);
if (superAdminExists) {
await req.db.getInviteTokenAndDelete(inviteToken);
}
const newUser = await req.db.insertUser({ ...req.body }, req.file); const newUser = await req.db.insertUser({ ...req.body }, req.file);
logger.info(successMessages.AUTH_CREATE_USER, { logger.info(successMessages.AUTH_CREATE_USER, {
service: SERVICE_NAME, service: SERVICE_NAME,
+2
View File
@@ -47,6 +47,7 @@ const {
const { const {
requestInviteToken, requestInviteToken,
getInviteToken, getInviteToken,
getInviteTokenAndDelete,
} = require("./modules/inviteModule"); } = require("./modules/inviteModule");
//**************************************** //****************************************
@@ -128,6 +129,7 @@ module.exports = {
logoutUser, logoutUser,
requestInviteToken, requestInviteToken,
getInviteToken, getInviteToken,
getInviteTokenAndDelete,
requestRecoveryToken, requestRecoveryToken,
validateRecoveryToken, validateRecoveryToken,
resetPassword, resetPassword,
+29 -1
View File
@@ -32,7 +32,7 @@ const requestInviteToken = async (userData) => {
}; };
/** /**
* Retrieves and deletes an invite token. * Retrieves an invite token
* *
* This function searches for an invite token in the database and deletes it. * This function searches for an invite token in the database and deletes it.
* If the invite token is not found, it throws an error. * If the invite token is not found, it throws an error.
@@ -42,6 +42,33 @@ const requestInviteToken = async (userData) => {
* @throws {Error} If the invite token is not found or there is another error. * @throws {Error} If the invite token is not found or there is another error.
*/ */
const getInviteToken = async (token) => { const getInviteToken = async (token) => {
console.log(token);
try {
const invite = await InviteToken.findOne({
token,
});
if (invite === null) {
throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND);
}
return invite;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getInviteToken";
throw error;
}
};
/**
* Retrieves and deletes an invite token
*
* This function searches for an invite token in the database and deletes it.
* If the invite token is not found, it throws an error.
*
* @param {string} token - The invite token to search for.
* @returns {Promise<InviteToken>} The invite token data.
* @throws {Error} If the invite token is not found or there is another error.
*/
const getInviteTokenAndDelete = async (token) => {
try { try {
const invite = await InviteToken.findOneAndDelete({ const invite = await InviteToken.findOneAndDelete({
token, token,
@@ -60,4 +87,5 @@ const getInviteToken = async (token) => {
module.exports = { module.exports = {
requestInviteToken, requestInviteToken,
getInviteToken, getInviteToken,
getInviteTokenAndDelete,
}; };
+2
View File
@@ -74,6 +74,7 @@ const registrationBodyValidation = joi.object({
.min(1) .min(1)
.required(), .required(),
teamId: joi.string().allow("").required(), teamId: joi.string().allow("").required(),
inviteToken: joi.string().allow("").required(),
}); });
const editUserParamValidation = joi.object({ const editUserParamValidation = joi.object({
@@ -137,6 +138,7 @@ const inviteBodyValidation = joi.object({
"string.email": "Must be a valid email address", "string.email": "Must be a valid email address",
}), }),
role: joi.array().required(), role: joi.array().required(),
teamId: joi.string().required(),
}); });
const inviteVerifciationBodyValidation = joi.object({ const inviteVerifciationBodyValidation = joi.object({