From a053386f4a18b161554fb4e7aabcd04dd2e82b62 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 6 Feb 2026 22:34:50 +0000 Subject: [PATCH 1/5] ui --- client/src/Pages/Auth/Register/index.tsx | 43 +++++++++++++++++++ .../Auth/Register/{index.jsx => old.jsx} | 0 client/src/Routes/index.jsx | 18 ++++++-- client/src/locales/en.json | 20 +++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 client/src/Pages/Auth/Register/index.tsx rename client/src/Pages/Auth/Register/{index.jsx => old.jsx} (100%) diff --git a/client/src/Pages/Auth/Register/index.tsx b/client/src/Pages/Auth/Register/index.tsx new file mode 100644 index 000000000..e51eff6ec --- /dev/null +++ b/client/src/Pages/Auth/Register/index.tsx @@ -0,0 +1,43 @@ +import { BaseAuthPage } from "@/Components/v2/design-elements"; +import { Button, TextField } from "@/Components/v2/inputs"; + +import { useTranslation } from "react-i18next"; +const RegisterPage = () => { + const { t } = useTranslation(); + return ( + + + + + + + + + ); +}; + +export default RegisterPage; diff --git a/client/src/Pages/Auth/Register/index.jsx b/client/src/Pages/Auth/Register/old.jsx similarity index 100% rename from client/src/Pages/Auth/Register/index.jsx rename to client/src/Pages/Auth/Register/old.jsx diff --git a/client/src/Routes/index.jsx b/client/src/Routes/index.jsx index acc9ad0c1..12716dc35 100644 --- a/client/src/Routes/index.jsx +++ b/client/src/Routes/index.jsx @@ -9,7 +9,8 @@ import NotFound from "../Pages/NotFound/index.jsx"; // Auth import AuthLogin from "../Pages/Auth/Login"; -import AuthRegister from "../Pages/Auth/Register/index.jsx"; +import AuthRegister from "@/Pages/Auth/Register"; +import OldAuthRegister from "../Pages/Auth/Register/old.jsx"; import AuthForgotPassword from "@/Pages/Auth/Recovery"; import AuthCheckEmail from "../Pages/Auth/CheckEmail.jsx"; import AuthSetNewPassword from "../Pages/Auth/SetNewPassword.jsx"; @@ -62,7 +63,8 @@ import CreateMonitor from "@/Pages/CreateMonitor"; const Routes = () => { const mode = useSelector((state) => state.ui.mode); - const AdminCheckedRegister = withAdminCheck(AuthRegister); + // const AdminCheckedRegister = withAdminCheck(AuthRegister); + const AdminCheckedRegister = AuthRegister; const v2theme = mode === "light" ? lightTheme : darkTheme; return ( @@ -372,7 +374,17 @@ const Routes = () => { } + element={ + <> + + + + + } + /> + } /> Date: Fri, 6 Feb 2026 22:53:41 +0000 Subject: [PATCH 2/5] form --- client/src/Hooks/useRegisterForm.ts | 17 ++++ client/src/Pages/Auth/Register/index.tsx | 99 ++++++++++++++++++++---- client/src/Validation/register.ts | 43 ++++++++++ 3 files changed, 143 insertions(+), 16 deletions(-) create mode 100644 client/src/Hooks/useRegisterForm.ts create mode 100644 client/src/Validation/register.ts diff --git a/client/src/Hooks/useRegisterForm.ts b/client/src/Hooks/useRegisterForm.ts new file mode 100644 index 000000000..a043d8ff6 --- /dev/null +++ b/client/src/Hooks/useRegisterForm.ts @@ -0,0 +1,17 @@ +import { registerSchema, type RegisterFormData } from "@/Validation/register"; + +export const useRegisterForm = () => { + const defaults: RegisterFormData = { + firstName: "", + lastName: "", + email: "", + password: "", + confirm: "", + role: ["superadmin"], + }; + + return { + schema: registerSchema, + defaults, + }; +}; diff --git a/client/src/Pages/Auth/Register/index.tsx b/client/src/Pages/Auth/Register/index.tsx index e51eff6ec..e2e4fe802 100644 --- a/client/src/Pages/Auth/Register/index.tsx +++ b/client/src/Pages/Auth/Register/index.tsx @@ -1,34 +1,101 @@ import { BaseAuthPage } from "@/Components/v2/design-elements"; import { Button, TextField } from "@/Components/v2/inputs"; - +import { useForm, Controller } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js"; +import { useRegisterForm } from "@/Hooks/useRegisterForm"; +import type { RegisterFormData } from "@/Validation/register"; import { useTranslation } from "react-i18next"; + const RegisterPage = () => { const { t } = useTranslation(); + const { schema, defaults } = useRegisterForm(); + + const { control, handleSubmit } = useForm({ + resolver: zodResolver(schema), + defaultValues: defaults, + }); + + const onSubmit = (data: RegisterFormData) => { + console.log("Registration data:", data); + }; + + const onError = (errors: any) => { + console.log("Validation errors:", errors); + }; + return ( - ( + + )} /> - ( + + )} /> - ( + + )} /> - ( + + )} /> - ( + + )} /> diff --git a/client/src/Types/User.ts b/client/src/Types/User.ts new file mode 100644 index 000000000..e09e87678 --- /dev/null +++ b/client/src/Types/User.ts @@ -0,0 +1,28 @@ +export const UserRoles = ["user", "admin", "superadmin", "demo"] as const; +export type UserRole = (typeof UserRoles)[number]; + +export interface UserProfileImage { + data?: Buffer; + contentType?: string; +} + +export interface User { + id: string; + firstName: string; + lastName: string; + email: string; + avatarImage?: string; + profileImage?: UserProfileImage; + isActive: boolean; + isVerified: boolean; + role: UserRole[]; + teamId: string; + checkTTL?: number; + createdAt: string; + updatedAt: string; +} + +export interface AuthResponse { + user: User; + token: string; +} diff --git a/client/src/Validation/register.ts b/client/src/Validation/register.ts index b32a8a2fc..67b425ecd 100644 --- a/client/src/Validation/register.ts +++ b/client/src/Validation/register.ts @@ -32,6 +32,8 @@ export const registerSchema = z (val) => specialCharPattern.test(val), "Password must contain a special character" ), + teamId: z.string().optional(), + inviteToken: z.string().optional(), confirm: z.string().min(1, "Please confirm your password"), role: z.array(z.string()).min(1, "Role is required"), }) diff --git a/server/src/service/business/userService.ts b/server/src/service/business/userService.ts index a0273af99..315c62bb0 100644 --- a/server/src/service/business/userService.ts +++ b/server/src/service/business/userService.ts @@ -90,6 +90,7 @@ class UserService { user.role = invite.role; user.teamId = invite.teamId; } else { + console.log("WHITFUSKALG"); // This is the first account, create JWT secret to use if one is not supplied by env const jwtSecret = this.crypto.randomBytes(64).toString("hex"); await this.settingsRepository.update({ jwtSecret }); From 5152336591750b5424f6aea3153c3aec6c9fee58 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 6 Feb 2026 23:31:07 +0000 Subject: [PATCH 4/5] fix validation, derrive teamID and Roles from tokens --- client/src/Hooks/useRegisterForm.ts | 3 -- client/src/Pages/Auth/Register/index.tsx | 37 ++++++++++++++++++++-- client/src/Routes/index.jsx | 8 ++++- client/src/Validation/register.ts | 3 -- server/src/service/business/userService.ts | 5 +-- server/src/validation/joi.ts | 4 +-- 6 files changed, 45 insertions(+), 15 deletions(-) diff --git a/client/src/Hooks/useRegisterForm.ts b/client/src/Hooks/useRegisterForm.ts index 3b5189995..99337a28b 100644 --- a/client/src/Hooks/useRegisterForm.ts +++ b/client/src/Hooks/useRegisterForm.ts @@ -7,9 +7,6 @@ export const useRegisterForm = () => { email: "", password: "", confirm: "", - teamId: "", - inviteToken: "", - role: ["superadmin"], }; return { diff --git a/client/src/Pages/Auth/Register/index.tsx b/client/src/Pages/Auth/Register/index.tsx index 99ef19951..80cee579f 100644 --- a/client/src/Pages/Auth/Register/index.tsx +++ b/client/src/Pages/Auth/Register/index.tsx @@ -8,11 +8,17 @@ import { useTranslation } from "react-i18next"; import { usePost } from "@/Hooks/UseApi"; import { setAuthState } from "@/Features/Auth/authSlice"; import { useDispatch } from "react-redux"; -import { useNavigate } from "react-router-dom"; +import { useNavigate, useParams } from "react-router-dom"; import type { AuthResponse } from "@/Types/User"; +import { useEffect, useRef } from "react"; interface RegisterPayload { user: Omit; + token?: string; +} + +interface InviteVerifyResponse { + email: string; } const RegisterPage = () => { @@ -21,17 +27,41 @@ const RegisterPage = () => { const { post, loading } = usePost(); const dispatch = useDispatch(); const navigate = useNavigate(); + const { token } = useParams<{ token?: string }>(); - const { control, handleSubmit, setError } = useForm({ + const { post: verifyToken } = usePost<{ token: string }, InviteVerifyResponse>(); + const hasVerified = useRef(false); + + const { control, handleSubmit, setError, reset } = useForm({ resolver: zodResolver(schema), defaultValues: defaults, }); + useEffect(() => { + if (!token || hasVerified.current) return; + hasVerified.current = true; + + verifyToken("/invite/verify", { token }).then((result) => { + if (result?.success && result?.data) { + reset({ + ...defaults, + email: result.data.email ?? "", + }); + } else { + navigate("/register", { replace: true }); + } + }); + }, [token]); + const onSubmit = async (data: RegisterFormData) => { if (loading) return; const { confirm, ...userData } = data; - const result = await post("/auth/register", { user: userData }); + const payload: RegisterPayload = { user: userData }; + if (token) { + payload.token = token; + } + const result = await post("/auth/register", payload); if (result?.success) { dispatch(setAuthState(result)); @@ -82,6 +112,7 @@ const RegisterPage = () => { render={({ field, fieldState }) => ( { } + element={ + <> + + + + + } /> specialCharPattern.test(val), "Password must contain a special character" ), - teamId: z.string().optional(), - inviteToken: z.string().optional(), confirm: z.string().min(1, "Please confirm your password"), - role: z.array(z.string()).min(1, "Role is required"), }) .refine((data) => data.password === data.confirm, { message: "Passwords do not match", diff --git a/server/src/service/business/userService.ts b/server/src/service/business/userService.ts index 315c62bb0..281c03ab7 100644 --- a/server/src/service/business/userService.ts +++ b/server/src/service/business/userService.ts @@ -87,10 +87,10 @@ class UserService { const superAdminExists = await this.usersRepository.findSuperAdmin(); if (superAdminExists) { const invite = await this.invitesRepository.findByTokenAndDelete(inviteToken); - user.role = invite.role; + user.role = invite.role ?? ["user"]; user.teamId = invite.teamId; + user.email = invite.email; } else { - console.log("WHITFUSKALG"); // This is the first account, create JWT secret to use if one is not supplied by env const jwtSecret = this.crypto.randomBytes(64).toString("hex"); await this.settingsRepository.update({ jwtSecret }); @@ -100,6 +100,7 @@ class UserService { } const team = await this.teamsRepository.create(user.email); user.teamId = team.id; + user.role = ["superadmin"]; } const newUser = await this.usersRepository.create({ ...user }, file); diff --git a/server/src/validation/joi.ts b/server/src/validation/joi.ts index 49f919a4a..c1e945961 100755 --- a/server/src/validation/joi.ts +++ b/server/src/validation/joi.ts @@ -51,9 +51,7 @@ const registrationBodyValidation = joi.object({ }), password: joi.string().min(8).required().pattern(passwordPattern), profileImage: joi.any(), - role: joi.array().items(joi.string().valid("superadmin", "admin", "user", "demo")), - teamId: joi.string().allow("").required(), - inviteToken: joi.string().allow("").required(), + inviteToken: joi.string().allow("").optional(), }); const editUserBodyValidation = joi.object({ From 734ec41ca0234acff2477b0d3ffd114c3a4277bd Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 6 Feb 2026 23:37:25 +0000 Subject: [PATCH 5/5] cleanup --- client/src/Pages/Auth/Register/old.jsx | 330 ------------------ .../src/Pages/Auth/components/AuthHeader.jsx | 49 --- .../Pages/Auth/components/AuthPageWrapper.jsx | 107 ------ client/src/Routes/index.jsx | 8 +- 4 files changed, 1 insertion(+), 493 deletions(-) delete mode 100644 client/src/Pages/Auth/Register/old.jsx delete mode 100644 client/src/Pages/Auth/components/AuthHeader.jsx delete mode 100644 client/src/Pages/Auth/components/AuthPageWrapper.jsx diff --git a/client/src/Pages/Auth/Register/old.jsx b/client/src/Pages/Auth/Register/old.jsx deleted file mode 100644 index 821c56427..000000000 --- a/client/src/Pages/Auth/Register/old.jsx +++ /dev/null @@ -1,330 +0,0 @@ -// Components -import Stack from "@mui/material/Stack"; -import Typography from "@mui/material/Typography"; -import TextInput from "@/Components/v1/Inputs/TextInput/index.jsx"; -import Button from "@mui/material/Button"; -import Box from "@mui/material/Box"; -import PasswordTooltip from "../components/PasswordTooltip.jsx"; - -// Utils -import { useTheme } from "@emotion/react"; -import { useTranslation } from "react-i18next"; -import { useState, useEffect } from "react"; -import { useNavigate } from "react-router-dom"; -import { useDispatch, useSelector } from "react-redux"; -import { useParams } from "react-router-dom"; -import { networkService } from "../../../main.jsx"; -import { newOrChangedCredentials } from "../../../Validation/validation.js"; -import { register } from "../../../Features/Auth/authSlice.js"; -import AuthPageWrapper from "../components/AuthPageWrapper.jsx"; -import { createToast } from "../../../Utils/toastUtils.jsx"; -import PropTypes from "prop-types"; - -const getFeedbackStatus = (form, errors, field, criteria) => { - const fieldErrors = errors?.[field]; - const isFieldEmpty = form?.[field]?.length === 0; - const hasError = fieldErrors?.includes(criteria) || fieldErrors?.includes("empty"); - const isCorrect = !isFieldEmpty && !hasError; - - if (isCorrect) { - return "success"; - } else if (hasError) { - return "error"; - } else { - return "info"; - } -}; - -const Register = ({ superAdminExists }) => { - // Redux - const { isLoading } = useSelector((state) => state.auth); - - // Local state - const [form, setForm] = useState({ - firstName: "", - lastName: "", - email: "", - password: "", - confirm: "", - role: [], - teamId: "", - }); - - const [errors, setErrors] = useState({}); - const [feedback, setFeedback] = useState({}); - - // Hooks - const theme = useTheme(); - const { t } = useTranslation(); - const { token } = useParams(); - const navigate = useNavigate(); - const dispatch = useDispatch(); - - // Effects - useEffect(() => { - const fetchInvite = async () => { - if (token !== undefined) { - try { - const res = await networkService.verifyInvitationToken(token); - const invite = res.data.data; - const { email } = invite; - setForm((prevForm) => { - if (!prevForm.email) { - return { ...prevForm, email }; - } - return prevForm; - }); - } catch (error) { - navigate("/register", { replace: true }); - } - } - }; - fetchInvite(); - }, [form, token, navigate]); - - // Handlers - const onChange = (e) => { - let { name, value } = e.target; - if (name === "email") value = value.toLowerCase(); - const updatedForm = { ...form, [name]: value }; - - const { error } = newOrChangedCredentials.validate( - { [name]: value }, - { abortEarly: false, context: { password: form.password } } - ); - - setErrors((prev) => ({ - ...prev, - [name]: error?.details?.[0]?.message || "", - })); - - setForm(updatedForm); - }; - - const onPasswordChange = (e) => { - const { name, value } = e.target; - const updatedForm = { ...form, [name]: value }; - setForm(updatedForm); - const validateValue = { [name]: value }; - const validateOptions = { abortEarly: false, context: { password: form.password } }; - if (name === "password" && form.confirm.length > 0) { - validateValue.confirm = form.confirm; - validateOptions.context = { password: value }; - } else if (name === "confirm") { - validateValue.password = form.password; - } - const { error } = newOrChangedCredentials.validate(validateValue, validateOptions); - - const pwdErrors = error?.details.map((error) => ({ - path: error.path[0], - type: error.type, - })); - - const errorsByPath = - pwdErrors && - pwdErrors.reduce((acc, { path, type }) => { - if (!acc[path]) { - acc[path] = []; - } - acc[path].push(type); - return acc; - }, {}); - - const oldErrors = { ...errors }; - if (name === "password") { - oldErrors.password = undefined; - } else if (name === "confirm") { - oldErrors.confirm = undefined; - } - const newErrors = { ...oldErrors, ...errorsByPath }; - - setErrors(newErrors); - - const newFeedback = { - length: getFeedbackStatus(updatedForm, errorsByPath, "password", "string.min"), - special: getFeedbackStatus(updatedForm, errorsByPath, "password", "special"), - number: getFeedbackStatus(updatedForm, errorsByPath, "password", "number"), - uppercase: getFeedbackStatus(updatedForm, errorsByPath, "password", "uppercase"), - lowercase: getFeedbackStatus(updatedForm, errorsByPath, "password", "lowercase"), - confirm: getFeedbackStatus(updatedForm, errorsByPath, "confirm", "different"), - }; - - setFeedback(newFeedback); - }; - - const onSubmit = async (e) => { - e.preventDefault(); - const toSubmit = { - ...form, - role: superAdminExists ? form.role : ["superadmin"], - inviteToken: token ? token : "", - }; - - const { error } = newOrChangedCredentials.validate(toSubmit, { - abortEarly: false, - context: { password: form.password }, - }); - - if (error) { - const formErrors = {}; - for (const err of error.details) { - formErrors[err.path[0]] = err.message; - } - setErrors(formErrors); - return; - } - - delete toSubmit.confirm; - - const action = await dispatch(register({ user: toSubmit, token })); - if (action.payload.success) { - navigate("/uptime"); - createToast({ - body: t("auth.registration.toasts.success"), - }); - } else { - if (action.payload) { - createToast({ - body: action.payload.msg, - }); - } else { - createToast({ - body: t("common.toasts.unknownError"), - }); - } - } - }; - - return ( - - - - {superAdminExists - ? t("auth.registration.description.user") - : t("auth.registration.description.superAdmin")} - - - - - - (e.target.value = e.target.value.toLowerCase())} - onChange={onChange} - error={errors.email ? true : false} - helperText={errors.email ? t(errors.email) : ""} // Localization keys are in validation.js - /> - - - - - - - - - - ); -}; - -Register.propTypes = { - superAdminExists: PropTypes.bool, -}; - -export default Register; diff --git a/client/src/Pages/Auth/components/AuthHeader.jsx b/client/src/Pages/Auth/components/AuthHeader.jsx deleted file mode 100644 index 56b69ef14..000000000 --- a/client/src/Pages/Auth/components/AuthHeader.jsx +++ /dev/null @@ -1,49 +0,0 @@ -// Components -import Stack from "@mui/material/Stack"; -import Typography from "@mui/material/Typography"; -import Logo from "@/assets/icons/checkmate-icon.svg?react"; -import LanguageSelector from "@/Components/LanguageSelector.jsx"; -import ThemeSwitch from "@/Components/v1/ThemeSwitch/index.jsx"; - -// Utils -import { useTheme } from "@mui/material/styles"; -import { useTranslation } from "react-i18next"; - -const AuthHeader = ({ hideLogo = false }) => { - // Hooks - const theme = useTheme(); - const { t } = useTranslation(); - - return ( - - - {!hideLogo && ( - <> - - {t("common.appName")} - - )} - - - - - - - ); -}; - -export default AuthHeader; diff --git a/client/src/Pages/Auth/components/AuthPageWrapper.jsx b/client/src/Pages/Auth/components/AuthPageWrapper.jsx deleted file mode 100644 index a5fbe311b..000000000 --- a/client/src/Pages/Auth/components/AuthPageWrapper.jsx +++ /dev/null @@ -1,107 +0,0 @@ -import Background from "@/assets/Images/background-grid.svg?react"; -import Stack from "@mui/material/Stack"; -import Box from "@mui/material/Box"; -import Typography from "@mui/material/Typography"; -import AuthHeader from "./AuthHeader.jsx"; -import { useTheme } from "@mui/material/styles"; -import Logo from "@/assets/icons/checkmate-icon.svg?react"; -import PropTypes from "prop-types"; - -const AuthPageWrapper = ({ children, heading, welcome }) => { - const theme = useTheme(); - return ( - - - - - - - - - - - - - - - - {welcome} - - {heading} - - {children} - - - ); -}; - -export default AuthPageWrapper; - -AuthPageWrapper.propTypes = { - children: PropTypes.node, - heading: PropTypes.node, - welcome: PropTypes.node, -}; diff --git a/client/src/Routes/index.jsx b/client/src/Routes/index.jsx index 71058c0c0..936258595 100644 --- a/client/src/Routes/index.jsx +++ b/client/src/Routes/index.jsx @@ -10,7 +10,6 @@ import NotFound from "../Pages/NotFound/index.jsx"; // Auth import AuthLogin from "../Pages/Auth/Login"; import AuthRegister from "@/Pages/Auth/Register"; -import OldAuthRegister from "../Pages/Auth/Register/old.jsx"; import AuthForgotPassword from "@/Pages/Auth/Recovery"; import AuthCheckEmail from "../Pages/Auth/CheckEmail.jsx"; import AuthSetNewPassword from "../Pages/Auth/SetNewPassword.jsx"; @@ -63,8 +62,7 @@ import CreateMonitor from "@/Pages/CreateMonitor"; const Routes = () => { const mode = useSelector((state) => state.ui.mode); - // const AdminCheckedRegister = withAdminCheck(AuthRegister); - const AdminCheckedRegister = AuthRegister; + const AdminCheckedRegister = withAdminCheck(AuthRegister); const v2theme = mode === "light" ? lightTheme : darkTheme; return ( @@ -382,10 +380,6 @@ const Routes = () => { } /> - } - />