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: "",
|
email: "",
|
||||||
password: "",
|
password: "",
|
||||||
confirm: "",
|
confirm: "",
|
||||||
teamId: "",
|
|
||||||
inviteToken: "",
|
|
||||||
role: ["superadmin"],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -8,11 +8,17 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { usePost } from "@/Hooks/UseApi";
|
import { usePost } from "@/Hooks/UseApi";
|
||||||
import { setAuthState } from "@/Features/Auth/authSlice";
|
import { setAuthState } from "@/Features/Auth/authSlice";
|
||||||
import { useDispatch } from "react-redux";
|
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 type { AuthResponse } from "@/Types/User";
|
||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
|
||||||
interface RegisterPayload {
|
interface RegisterPayload {
|
||||||
user: Omit<RegisterFormData, "confirm">;
|
user: Omit<RegisterFormData, "confirm">;
|
||||||
|
token?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InviteVerifyResponse {
|
||||||
|
email: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const RegisterPage = () => {
|
const RegisterPage = () => {
|
||||||
@@ -21,17 +27,41 @@ const RegisterPage = () => {
|
|||||||
const { post, loading } = usePost<RegisterPayload, AuthResponse>();
|
const { post, loading } = usePost<RegisterPayload, AuthResponse>();
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
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),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: defaults,
|
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) => {
|
const onSubmit = async (data: RegisterFormData) => {
|
||||||
if (loading) return;
|
if (loading) return;
|
||||||
|
|
||||||
const { confirm, ...userData } = data;
|
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) {
|
if (result?.success) {
|
||||||
dispatch(setAuthState(result));
|
dispatch(setAuthState(result));
|
||||||
@@ -82,6 +112,7 @@ const RegisterPage = () => {
|
|||||||
render={({ field, fieldState }) => (
|
render={({ field, fieldState }) => (
|
||||||
<TextField
|
<TextField
|
||||||
{...field}
|
{...field}
|
||||||
|
disabled={!!token}
|
||||||
fieldLabel={t("pages.auth.common.form.option.email.label")}
|
fieldLabel={t("pages.auth.common.form.option.email.label")}
|
||||||
placeholder={t("pages.auth.common.form.option.email.placeholder")}
|
placeholder={t("pages.auth.common.form.option.email.placeholder")}
|
||||||
error={!!fieldState.error}
|
error={!!fieldState.error}
|
||||||
|
|||||||
@@ -390,7 +390,13 @@ const Routes = () => {
|
|||||||
<Route
|
<Route
|
||||||
exact
|
exact
|
||||||
path="/register/:token"
|
path="/register/:token"
|
||||||
element={<AuthRegister superAdminExists={true} />}
|
element={
|
||||||
|
<>
|
||||||
|
<ThemeProvider theme={v2theme}>
|
||||||
|
<AuthRegister superAdminExists={true} />
|
||||||
|
</ThemeProvider>
|
||||||
|
</>
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Route
|
<Route
|
||||||
|
|||||||
@@ -32,10 +32,7 @@ export const registerSchema = z
|
|||||||
(val) => specialCharPattern.test(val),
|
(val) => specialCharPattern.test(val),
|
||||||
"Password must contain a special character"
|
"Password must contain a special character"
|
||||||
),
|
),
|
||||||
teamId: z.string().optional(),
|
|
||||||
inviteToken: z.string().optional(),
|
|
||||||
confirm: z.string().min(1, "Please confirm your password"),
|
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, {
|
.refine((data) => data.password === data.confirm, {
|
||||||
message: "Passwords do not match",
|
message: "Passwords do not match",
|
||||||
|
|||||||
@@ -87,10 +87,10 @@ class UserService {
|
|||||||
const superAdminExists = await this.usersRepository.findSuperAdmin();
|
const superAdminExists = await this.usersRepository.findSuperAdmin();
|
||||||
if (superAdminExists) {
|
if (superAdminExists) {
|
||||||
const invite = await this.invitesRepository.findByTokenAndDelete(inviteToken);
|
const invite = await this.invitesRepository.findByTokenAndDelete(inviteToken);
|
||||||
user.role = invite.role;
|
user.role = invite.role ?? ["user"];
|
||||||
user.teamId = invite.teamId;
|
user.teamId = invite.teamId;
|
||||||
|
user.email = invite.email;
|
||||||
} else {
|
} else {
|
||||||
console.log("WHITFUSKALG");
|
|
||||||
// This is the first account, create JWT secret to use if one is not supplied by env
|
// 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");
|
const jwtSecret = this.crypto.randomBytes(64).toString("hex");
|
||||||
await this.settingsRepository.update({ jwtSecret });
|
await this.settingsRepository.update({ jwtSecret });
|
||||||
@@ -100,6 +100,7 @@ class UserService {
|
|||||||
}
|
}
|
||||||
const team = await this.teamsRepository.create(user.email);
|
const team = await this.teamsRepository.create(user.email);
|
||||||
user.teamId = team.id;
|
user.teamId = team.id;
|
||||||
|
user.role = ["superadmin"];
|
||||||
}
|
}
|
||||||
|
|
||||||
const newUser = await this.usersRepository.create({ ...user }, file);
|
const newUser = await this.usersRepository.create({ ...user }, file);
|
||||||
|
|||||||
@@ -51,9 +51,7 @@ const registrationBodyValidation = joi.object({
|
|||||||
}),
|
}),
|
||||||
password: joi.string().min(8).required().pattern(passwordPattern),
|
password: joi.string().min(8).required().pattern(passwordPattern),
|
||||||
profileImage: joi.any(),
|
profileImage: joi.any(),
|
||||||
role: joi.array().items(joi.string().valid("superadmin", "admin", "user", "demo")),
|
inviteToken: joi.string().allow("").optional(),
|
||||||
teamId: joi.string().allow("").required(),
|
|
||||||
inviteToken: joi.string().allow("").required(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const editUserBodyValidation = joi.object({
|
const editUserBodyValidation = joi.object({
|
||||||
|
|||||||
Reference in new issue
Block a user