Added check to verify and fetch token from server
This commit is contained in:
1 parent
39d78ac4d7
commit
c77414f2d7
7 files changed
+61
-3
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { Stack, Typography } from "@mui/material";
|
import { Stack, Typography } from "@mui/material";
|
||||||
@@ -15,13 +15,13 @@ import { createToast } from "../../../Utils/toastUtils";
|
|||||||
import Field from "../../../Components/Inputs/Field";
|
import Field from "../../../Components/Inputs/Field";
|
||||||
import { register } from "../../../Features/Auth/authSlice";
|
import { register } from "../../../Features/Auth/authSlice";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
import axiosInstance from "../../../Utils/axiosConfig";
|
||||||
|
|
||||||
const Register = ({ isAdmin }) => {
|
const Register = ({ isAdmin }) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { token } = useParams();
|
const { token } = useParams();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
// TODO If possible, change the IDs of these fields to match the backend
|
// TODO If possible, change the IDs of these fields to match the backend
|
||||||
const idMap = {
|
const idMap = {
|
||||||
"register-firstname-input": "firstName",
|
"register-firstname-input": "firstName",
|
||||||
@@ -41,6 +41,23 @@ const Register = ({ isAdmin }) => {
|
|||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchInvite = async () => {
|
||||||
|
if (token !== undefined) {
|
||||||
|
try {
|
||||||
|
const res = await axiosInstance.post(`/auth/invite/verify`, {
|
||||||
|
token,
|
||||||
|
});
|
||||||
|
const { role, email } = res.data.data;
|
||||||
|
setForm({ ...form, email, role });
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
fetchInvite();
|
||||||
|
}, [token]);
|
||||||
|
|
||||||
const handleSubmit = async (e) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ const credentials = joi.object({
|
|||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}),
|
}),
|
||||||
role: joi.array().required(),
|
role: joi.array(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const monitorValidation = joi.object({
|
const monitorValidation = joi.object({
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const {
|
|||||||
deleteUserParamValidation,
|
deleteUserParamValidation,
|
||||||
inviteRoleValidation,
|
inviteRoleValidation,
|
||||||
inviteBodyValidation,
|
inviteBodyValidation,
|
||||||
|
inviteVerifciationBodyValidation,
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
@@ -237,6 +238,21 @@ const inviteController = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const inviteVerifyController = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
await inviteVerifciationBodyValidation.validateAsync(req.body);
|
||||||
|
const invite = await req.db.getInviteToken(req, res);
|
||||||
|
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ status: "success", msg: "Invite verified", data: invite });
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks to see if an admin account exists
|
* Checks to see if an admin account exists
|
||||||
* @async
|
* @async
|
||||||
@@ -445,6 +461,7 @@ module.exports = {
|
|||||||
loginController,
|
loginController,
|
||||||
userEditController,
|
userEditController,
|
||||||
inviteController,
|
inviteController,
|
||||||
|
inviteVerifyController,
|
||||||
checkAdminController,
|
checkAdminController,
|
||||||
recoveryRequestController,
|
recoveryRequestController,
|
||||||
validateRecoveryTokenController,
|
validateRecoveryTokenController,
|
||||||
|
|||||||
@@ -185,6 +185,21 @@ const requestInviteToken = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getInviteToken = async (req, res) => {
|
||||||
|
try {
|
||||||
|
console.log(req.body.token);
|
||||||
|
const invite = await InviteToken.findOneAndDelete({
|
||||||
|
token: req.body.token,
|
||||||
|
});
|
||||||
|
if (invite === null) {
|
||||||
|
throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND);
|
||||||
|
}
|
||||||
|
return invite;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request a recovery token
|
* Request a recovery token
|
||||||
* @async
|
* @async
|
||||||
@@ -695,6 +710,7 @@ module.exports = {
|
|||||||
deleteUser,
|
deleteUser,
|
||||||
getAllUsers,
|
getAllUsers,
|
||||||
requestInviteToken,
|
requestInviteToken,
|
||||||
|
getInviteToken,
|
||||||
requestRecoveryToken,
|
requestRecoveryToken,
|
||||||
validateRecoveryToken,
|
validateRecoveryToken,
|
||||||
resetPassword,
|
resetPassword,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const {
|
|||||||
getAllUsersController,
|
getAllUsersController,
|
||||||
deleteUserController,
|
deleteUserController,
|
||||||
inviteController,
|
inviteController,
|
||||||
|
inviteVerifyController,
|
||||||
} = require("../controllers/authController");
|
} = require("../controllers/authController");
|
||||||
|
|
||||||
//Auth routes
|
//Auth routes
|
||||||
@@ -38,6 +39,7 @@ router.delete(
|
|||||||
);
|
);
|
||||||
|
|
||||||
router.post("/invite", verifyJWT, inviteController);
|
router.post("/invite", verifyJWT, inviteController);
|
||||||
|
router.post("/invite/verify", inviteVerifyController);
|
||||||
|
|
||||||
//Recovery routes
|
//Recovery routes
|
||||||
router.post("/recovery/request", recoveryRequestController);
|
router.post("/recovery/request", recoveryRequestController);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const errorMessages = {
|
|||||||
// Auth Controller
|
// Auth Controller
|
||||||
UNAUTHORIZED: "Unauthorized access",
|
UNAUTHORIZED: "Unauthorized access",
|
||||||
AUTH_ADMIN_EXISTS: "Admin already exists",
|
AUTH_ADMIN_EXISTS: "Admin already exists",
|
||||||
|
AUTH_INVITE_NOT_FOUND: "Invite not found",
|
||||||
|
|
||||||
//Error handling middleware
|
//Error handling middleware
|
||||||
UNKNOWN_SERVICE: "Unknown service",
|
UNKNOWN_SERVICE: "Unknown service",
|
||||||
|
|||||||
@@ -112,6 +112,10 @@ const inviteBodyValidation = joi.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const inviteVerifciationBodyValidation = joi.object({
|
||||||
|
token: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Monitors
|
// Monitors
|
||||||
//****************************************
|
//****************************************
|
||||||
@@ -239,6 +243,7 @@ module.exports = {
|
|||||||
newPasswordValidation,
|
newPasswordValidation,
|
||||||
inviteRoleValidation,
|
inviteRoleValidation,
|
||||||
inviteBodyValidation,
|
inviteBodyValidation,
|
||||||
|
inviteVerifciationBodyValidation,
|
||||||
getMonitorByIdValidation,
|
getMonitorByIdValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
monitorValidation,
|
monitorValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user