fix(route)
This commit is contained in:
parent
29070d2748
commit
9f75edf36c
4
constant/route.constant.ts
Normal file
4
constant/route.constant.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export const routes = {
|
||||
LOGIN: '/login',
|
||||
PRODUCTS: '/products',
|
||||
};
|
@ -19,6 +19,7 @@ import { useRouter } from "next/navigation";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { RootState } from "@/services/store";
|
||||
import { setAuthTokens, setUserDetails } from "@/services/store/authSlice";
|
||||
import { routes } from "constant/route.constant";
|
||||
|
||||
const loginSchema = z.object({
|
||||
username: z.string(),
|
||||
@ -49,7 +50,7 @@ export default function LoginPage() {
|
||||
);
|
||||
dispatch(setUserDetails(response));
|
||||
setLoader(false)
|
||||
router.push('/home');
|
||||
router.push(routes.PRODUCTS);
|
||||
} catch (err) {
|
||||
setLoader(false)
|
||||
setError('Invalid credentials');
|
@ -8,6 +8,7 @@ import store from "@/services/store";
|
||||
import AuthGuard from "@/hoc/authGuard/authGuard";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
import { routes } from "constant/route.constant";
|
||||
|
||||
interface RootLayoutProps {
|
||||
children: React.ReactNode;
|
||||
@ -21,9 +22,9 @@ function RootLayout(props: RootLayoutProps): JSX.Element {
|
||||
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
router.push('/log-in');
|
||||
router.push(routes.LOGIN);
|
||||
} else {
|
||||
router.replace('/home');
|
||||
router.replace(routes.PRODUCTS);
|
||||
}
|
||||
}, [router])
|
||||
return (
|
||||
|
@ -22,6 +22,7 @@ import { BUILDING, GEAR } from "@/utilities/svgConstant";
|
||||
import LogoutIcon from "@mui/icons-material/Logout";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useSelector } from "react-redux";
|
||||
import { routes } from "constant/route.constant";
|
||||
|
||||
const drawerWidth = 260;
|
||||
|
||||
@ -133,7 +134,7 @@ export default function DashboardWrapper(props: Props) {
|
||||
const logoutHandler = () => {
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("refreshToken");
|
||||
router.push("/log-in");
|
||||
router.push(routes.LOGIN);
|
||||
};
|
||||
|
||||
return (
|
||||
|
@ -1,4 +1,5 @@
|
||||
"use client"
|
||||
import { routes } from 'constant/route.constant';
|
||||
// hoc/withAuth.tsx
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useEffect } from 'react';
|
||||
@ -10,7 +11,7 @@ const AuthGuard = (WrappedComponent: any) => {
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
router.push('/log-in');
|
||||
router.push(routes.LOGIN);
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
// axiosInstance.ts
|
||||
import axios from 'axios';
|
||||
import { routes } from 'constant/route.constant';
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: process.env.NEXT_PUBLIC_API_URL,
|
||||
@ -55,7 +56,7 @@ axiosInstance.interceptors.response.use(
|
||||
|
||||
const refreshToken = localStorage.getItem('refreshToken');
|
||||
if (!refreshToken) {
|
||||
window.location.href = '/log-in';
|
||||
window.location.href = routes.LOGIN;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
@ -74,7 +75,7 @@ axiosInstance.interceptors.response.use(
|
||||
processQueue(err, null);
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('refreshToken');
|
||||
window.location.href = '/login';
|
||||
window.location.href = routes.LOGIN;
|
||||
return Promise.reject(err);
|
||||
} finally {
|
||||
isRefreshing = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user