mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-13 13:19:42 -06:00
Added check to verify and fetch token from server
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useTheme } from "@emotion/react";
|
||||
import { Stack, Typography } from "@mui/material";
|
||||
@@ -15,13 +15,13 @@ import { createToast } from "../../../Utils/toastUtils";
|
||||
import Field from "../../../Components/Inputs/Field";
|
||||
import { register } from "../../../Features/Auth/authSlice";
|
||||
import { useParams } from "react-router-dom";
|
||||
import axiosInstance from "../../../Utils/axiosConfig";
|
||||
|
||||
const Register = ({ isAdmin }) => {
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const { token } = useParams();
|
||||
const theme = useTheme();
|
||||
|
||||
// TODO If possible, change the IDs of these fields to match the backend
|
||||
const idMap = {
|
||||
"register-firstname-input": "firstName",
|
||||
@@ -41,6 +41,23 @@ const Register = ({ isAdmin }) => {
|
||||
});
|
||||
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) => {
|
||||
e.preventDefault();
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ const credentials = joi.object({
|
||||
}
|
||||
return value;
|
||||
}),
|
||||
role: joi.array().required(),
|
||||
role: joi.array(),
|
||||
});
|
||||
|
||||
const monitorValidation = joi.object({
|
||||
|
||||
@@ -10,6 +10,7 @@ const {
|
||||
deleteUserParamValidation,
|
||||
inviteRoleValidation,
|
||||
inviteBodyValidation,
|
||||
inviteVerifciationBodyValidation,
|
||||
} = require("../validation/joi");
|
||||
const logger = require("../utils/logger");
|
||||
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
|
||||
* @async
|
||||
@@ -445,6 +461,7 @@ module.exports = {
|
||||
loginController,
|
||||
userEditController,
|
||||
inviteController,
|
||||
inviteVerifyController,
|
||||
checkAdminController,
|
||||
recoveryRequestController,
|
||||
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
|
||||
* @async
|
||||
@@ -695,6 +710,7 @@ module.exports = {
|
||||
deleteUser,
|
||||
getAllUsers,
|
||||
requestInviteToken,
|
||||
getInviteToken,
|
||||
requestRecoveryToken,
|
||||
validateRecoveryToken,
|
||||
resetPassword,
|
||||
|
||||
@@ -17,6 +17,7 @@ const {
|
||||
getAllUsersController,
|
||||
deleteUserController,
|
||||
inviteController,
|
||||
inviteVerifyController,
|
||||
} = require("../controllers/authController");
|
||||
|
||||
//Auth routes
|
||||
@@ -38,6 +39,7 @@ router.delete(
|
||||
);
|
||||
|
||||
router.post("/invite", verifyJWT, inviteController);
|
||||
router.post("/invite/verify", inviteVerifyController);
|
||||
|
||||
//Recovery routes
|
||||
router.post("/recovery/request", recoveryRequestController);
|
||||
|
||||
@@ -6,6 +6,7 @@ const errorMessages = {
|
||||
// Auth Controller
|
||||
UNAUTHORIZED: "Unauthorized access",
|
||||
AUTH_ADMIN_EXISTS: "Admin already exists",
|
||||
AUTH_INVITE_NOT_FOUND: "Invite not found",
|
||||
|
||||
//Error handling middleware
|
||||
UNKNOWN_SERVICE: "Unknown service",
|
||||
|
||||
@@ -112,6 +112,10 @@ const inviteBodyValidation = joi.object({
|
||||
}),
|
||||
});
|
||||
|
||||
const inviteVerifciationBodyValidation = joi.object({
|
||||
token: joi.string().required(),
|
||||
});
|
||||
|
||||
//****************************************
|
||||
// Monitors
|
||||
//****************************************
|
||||
@@ -239,6 +243,7 @@ module.exports = {
|
||||
newPasswordValidation,
|
||||
inviteRoleValidation,
|
||||
inviteBodyValidation,
|
||||
inviteVerifciationBodyValidation,
|
||||
getMonitorByIdValidation,
|
||||
getMonitorsByUserIdValidation,
|
||||
monitorValidation,
|
||||
|
||||
Reference in New Issue
Block a user