login page refactor
This commit is contained in:
1 parent
ea68e660fc
commit
514570da78
3 files changed
+35
-18
No files matched your search
@@ -196,6 +196,13 @@ const authSlice = createSlice({
|
|||||||
state.success = true;
|
state.success = true;
|
||||||
state.msg = "Logged out successfully";
|
state.msg = "Logged out successfully";
|
||||||
},
|
},
|
||||||
|
setAuthState: (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
state.authToken = action.payload.data.token;
|
||||||
|
state.user = action.payload.data.user;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
// Register thunk
|
// Register thunk
|
||||||
@@ -249,4 +256,4 @@ const authSlice = createSlice({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default authSlice.reducer;
|
export default authSlice.reducer;
|
||||||
export const { clearAuthState } = authSlice.actions;
|
export const { clearAuthState, setAuthState } = authSlice.actions;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAuthPage, TextLink } from "@/Components/v2/design-elements";
|
import { BaseAuthPage } from "@/Components/v2/design-elements";
|
||||||
import { Button, TextField } from "@/Components/v2/inputs";
|
import { Button, TextField } from "@/Components/v2/inputs";
|
||||||
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
@@ -6,19 +6,38 @@ import { useForm, Controller } from "react-hook-form";
|
|||||||
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
|
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
|
||||||
import { useLoginForm } from "@/Hooks/useLoginForm";
|
import { useLoginForm } from "@/Hooks/useLoginForm";
|
||||||
import type { LoginFormData } from "@/Validation/login";
|
import type { LoginFormData } from "@/Validation/login";
|
||||||
|
import { useDispatch } from "react-redux";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { setAuthState } from "@/Features/Auth/authSlice";
|
||||||
|
import { usePost } from "@/Hooks/UseApi";
|
||||||
|
|
||||||
const LoginPage = () => {
|
const LoginPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const { post, loading } = usePost();
|
||||||
|
|
||||||
const { schema, defaults } = useLoginForm();
|
const { schema, defaults } = useLoginForm();
|
||||||
|
|
||||||
const { control, handleSubmit } = useForm<LoginFormData>({
|
const { control, handleSubmit, setError } = useForm<LoginFormData>({
|
||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = (data: LoginFormData) => {
|
const onSubmit = async (data: LoginFormData) => {
|
||||||
console.log("Login submitted:", data);
|
if (loading) return;
|
||||||
|
|
||||||
|
const result = await post("/auth/login", data);
|
||||||
|
|
||||||
|
if (result?.success) {
|
||||||
|
console.log(result);
|
||||||
|
// dispatch(setAuthState(result));
|
||||||
|
// navigate("/uptime");
|
||||||
|
} else if (result?.msg === "Incorrect password") {
|
||||||
|
setError("password", {
|
||||||
|
message: "auth.login.errors.password.incorrect",
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -58,21 +77,10 @@ const LoginPage = () => {
|
|||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
type="submit"
|
type="submit"
|
||||||
|
loading={loading}
|
||||||
>
|
>
|
||||||
{t("pages.auth.login.submit")}
|
{t("pages.auth.login.submit")}
|
||||||
</Button>
|
</Button>
|
||||||
<TextLink
|
|
||||||
alignSelf={"center"}
|
|
||||||
text={t("pages.auth.login.links.forgotPassword.text")}
|
|
||||||
linkText={t("pages.auth.login.links.forgotPassword.linkText")}
|
|
||||||
href="/forgot-password"
|
|
||||||
/>
|
|
||||||
<TextLink
|
|
||||||
alignSelf={"center"}
|
|
||||||
text={t("pages.auth.login.links.register.text")}
|
|
||||||
linkText={t("pages.auth.login.links.register.linkText")}
|
|
||||||
href="/register"
|
|
||||||
/>
|
|
||||||
</BaseAuthPage>
|
</BaseAuthPage>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -42,7 +42,9 @@ export const initApiClient = (store: StoreType): void => {
|
|||||||
const onSuccess = (response: AxiosResponse) => response;
|
const onSuccess = (response: AxiosResponse) => response;
|
||||||
const onError = (error: AxiosError) => {
|
const onError = (error: AxiosError) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
window.location.href = "/login";
|
if (window.location.pathname !== "/login") {
|
||||||
|
window.location.href = "/login";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user