fix problem cache uer
This commit is contained in:
@@ -5,14 +5,17 @@ import { LoginFormInterface } from "../../types"
|
||||
import { loginSchema } from "../../schema"
|
||||
import { ErrorComponent } from "../common/error"
|
||||
import { yupResolver } from '@hookform/resolvers/yup'
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { loginMutation } from "../../services/api/auth"
|
||||
import { ButtonComponent } from "../common/button"
|
||||
import toast from "react-hot-toast"
|
||||
import { handleError } from "../../utility/error-handle"
|
||||
import { useUserStore } from "../../store/user"
|
||||
|
||||
export const LoginForm = () => {
|
||||
const navigate = useNavigate()
|
||||
const { clearUser } = useUserStore()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const { mutate, isPending } = useMutation({
|
||||
mutationFn: loginMutation,
|
||||
@@ -33,6 +36,8 @@ export const LoginForm = () => {
|
||||
remember_me: true
|
||||
}, {
|
||||
onSuccess: async (data: any) => {
|
||||
clearUser()
|
||||
queryClient.clear()
|
||||
window.localStorage.setItem("token", data?.data?.token)
|
||||
toast.success("با موفقیت وارد شدین")
|
||||
navigate("/")
|
||||
|
||||
@@ -7,13 +7,16 @@ import { yupResolver } from "@hookform/resolvers/yup"
|
||||
import { LoginCodeSchema } from "../../schema"
|
||||
import { useEffect, useState } from "react"
|
||||
import { ButtonComponent } from "../common/button"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { loginWithOTP, sendOTPMutation } from "../../services/api/auth"
|
||||
import toast from "react-hot-toast"
|
||||
import { handleError } from "../../utility/error-handle"
|
||||
import { useUserStore } from "../../store/user"
|
||||
|
||||
export const SendCodeForm = () => {
|
||||
const navigate = useNavigate()
|
||||
const { clearUser } = useUserStore()
|
||||
const queryClient = useQueryClient()
|
||||
const { mutate, isPending } = useMutation({
|
||||
mutationFn: sendOTPMutation,
|
||||
mutationKey: ["send-otp"]
|
||||
@@ -49,8 +52,10 @@ export const SendCodeForm = () => {
|
||||
},
|
||||
{
|
||||
onSuccess: (res: any) => {
|
||||
toast.success("با موفقیت وارد شدید")
|
||||
clearUser()
|
||||
queryClient.clear()
|
||||
window.localStorage.setItem("token", res?.data?.token)
|
||||
toast.success("با موفقیت وارد شدید")
|
||||
navigate("/")
|
||||
setSeconds(30)
|
||||
setIsTimeUp(false)
|
||||
|
||||
@@ -5,6 +5,7 @@ import { SidebarItem } from "./sidebar-item";
|
||||
import { CloseCircle, LogoutCurve } from "iconsax-react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useUserStore } from "../../../store/user";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
type Props = {
|
||||
setShow: (value: boolean) => void,
|
||||
@@ -14,11 +15,13 @@ type Props = {
|
||||
export const SidebarMobile: FC<Props> = ({ setShow, show }) => {
|
||||
const { clearUser } = useUserStore()
|
||||
const navigate = useNavigate()
|
||||
const queryClient = useQueryClient()
|
||||
const handleCloseSidebar = () => setShow(false);
|
||||
const handleExit = () => {
|
||||
window.localStorage.clear()
|
||||
navigate("/auth/login")
|
||||
clearUser()
|
||||
queryClient.clear()
|
||||
navigate("/auth/login")
|
||||
}
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -3,11 +3,17 @@ import { SidebarInterface } from "../../../types"
|
||||
import { sidebarItems } from "../../../utility/sidebar"
|
||||
import { SidebarItem } from "./sidebar-item"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
import { useUserStore } from "../../../store/user"
|
||||
import { useQueryClient } from "@tanstack/react-query"
|
||||
|
||||
export const Sidebar = () => {
|
||||
const navigate = useNavigate()
|
||||
const { clearUser } = useUserStore()
|
||||
const queryClient = useQueryClient()
|
||||
const handleExit = () => {
|
||||
window.localStorage.clear()
|
||||
clearUser()
|
||||
queryClient.clear()
|
||||
navigate("/auth/login")
|
||||
}
|
||||
return (
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import toast from "react-hot-toast";
|
||||
import { handleLogout } from "./logout";
|
||||
|
||||
export const handleError = (err: any) => {
|
||||
if (err) {
|
||||
const errorStatus: number = err?.response?.status;
|
||||
const keys = errorStatus === 422 ? Object.keys(err?.response?.data.validation) : [];
|
||||
const handle401Error = () => {
|
||||
window.localStorage.clear()
|
||||
window.location.href = "/auth/login"
|
||||
handleLogout();
|
||||
}
|
||||
switch (errorStatus) {
|
||||
case 422: return toast.error(err?.response?.data?.validation[keys[0]]?.msg);
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { QueryClient } from "@tanstack/react-query";
|
||||
|
||||
export const handleLogout = (queryClient?: QueryClient) => {
|
||||
window.localStorage.clear();
|
||||
if (queryClient) {
|
||||
queryClient.clear();
|
||||
}
|
||||
window.location.href = "/auth/login";
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user