fix validation, derrive teamID and Roles from tokens
This commit is contained in:
1 parent
78b920aaf6
commit
5152336591
6 files changed
+45
-15
No files matched your search
@@ -7,9 +7,6 @@ export const useRegisterForm = () => {
|
||||
email: "",
|
||||
password: "",
|
||||
confirm: "",
|
||||
teamId: "",
|
||||
inviteToken: "",
|
||||
role: ["superadmin"],
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@@ -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<RegisterFormData, "confirm">;
|
||||
token?: string;
|
||||
}
|
||||
|
||||
interface InviteVerifyResponse {
|
||||
email: string;
|
||||
}
|
||||
|
||||
const RegisterPage = () => {
|
||||
@@ -21,17 +27,41 @@ const RegisterPage = () => {
|
||||
const { post, loading } = usePost<RegisterPayload, AuthResponse>();
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const { token } = useParams<{ token?: string }>();
|
||||
|
||||
const { control, handleSubmit, setError } = useForm<RegisterFormData>({
|
||||
const { post: verifyToken } = usePost<{ token: string }, InviteVerifyResponse>();
|
||||
const hasVerified = useRef(false);
|
||||
|
||||
const { control, handleSubmit, setError, reset } = useForm<RegisterFormData>({
|
||||
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 }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
disabled={!!token}
|
||||
fieldLabel={t("pages.auth.common.form.option.email.label")}
|
||||
placeholder={t("pages.auth.common.form.option.email.placeholder")}
|
||||
error={!!fieldState.error}
|
||||
|
||||
@@ -390,7 +390,13 @@ const Routes = () => {
|
||||
<Route
|
||||
exact
|
||||
path="/register/:token"
|
||||
element={<AuthRegister superAdminExists={true} />}
|
||||
element={
|
||||
<>
|
||||
<ThemeProvider theme={v2theme}>
|
||||
<AuthRegister superAdminExists={true} />
|
||||
</ThemeProvider>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
<Route
|
||||
|
||||
@@ -32,10 +32,7 @@ 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"),
|
||||
})
|
||||
.refine((data) => data.password === data.confirm, {
|
||||
message: "Passwords do not match",
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in new issue
Block a user