Browse Source

fix(route)

master
Prakash Maity 6 months ago
parent
commit
9f75edf36c
9 changed files with 16 additions and 7 deletions
  1. +4
    -0
      constant/route.constant.ts
  2. +0
    -0
      src/app/(auth)/login/layout.tsx
  3. +0
    -0
      src/app/(auth)/login/loginPage.module.scss
  4. +2
    -1
      src/app/(auth)/login/page.tsx
  5. +0
    -0
      src/app/(dashboard)/products/page.tsx
  6. +3
    -2
      src/app/layout.tsx
  7. +2
    -1
      src/components/wrapper/dashboardWrapper.tsx
  8. +2
    -1
      src/hoc/authGuard/authGuard.tsx
  9. +3
    -2
      src/services/axios/axiosInstance.ts

+ 4
- 0
constant/route.constant.ts View File

@ -0,0 +1,4 @@
export const routes = {
LOGIN: '/login',
PRODUCTS: '/products',
};

src/app/(auth)/log-in/layout.tsx → src/app/(auth)/login/layout.tsx View File


src/app/(auth)/log-in/loginPage.module.scss → src/app/(auth)/login/loginPage.module.scss View File


src/app/(auth)/log-in/page.tsx → src/app/(auth)/login/page.tsx View File

@ -19,6 +19,7 @@ import { useRouter } from "next/navigation";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { RootState } from "@/services/store"; import { RootState } from "@/services/store";
import { setAuthTokens, setUserDetails } from "@/services/store/authSlice"; import { setAuthTokens, setUserDetails } from "@/services/store/authSlice";
import { routes } from "constant/route.constant";
const loginSchema = z.object({ const loginSchema = z.object({
username: z.string(), username: z.string(),
@ -49,7 +50,7 @@ export default function LoginPage() {
); );
dispatch(setUserDetails(response)); dispatch(setUserDetails(response));
setLoader(false) setLoader(false)
router.push('/home');
router.push(routes.PRODUCTS);
} catch (err) { } catch (err) {
setLoader(false) setLoader(false)
setError('Invalid credentials'); setError('Invalid credentials');

src/app/(dashboard)/home/page.tsx → src/app/(dashboard)/products/page.tsx View File


+ 3
- 2
src/app/layout.tsx View File

@ -8,6 +8,7 @@ import store from "@/services/store";
import AuthGuard from "@/hoc/authGuard/authGuard"; import AuthGuard from "@/hoc/authGuard/authGuard";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useEffect } from "react"; import { useEffect } from "react";
import { routes } from "constant/route.constant";
interface RootLayoutProps { interface RootLayoutProps {
children: React.ReactNode; children: React.ReactNode;
@ -21,9 +22,9 @@ function RootLayout(props: RootLayoutProps): JSX.Element {
const token = localStorage.getItem('token'); const token = localStorage.getItem('token');
if (!token) { if (!token) {
router.push('/log-in');
router.push(routes.LOGIN);
} else { } else {
router.replace('/home');
router.replace(routes.PRODUCTS);
} }
}, [router]) }, [router])
return ( return (


+ 2
- 1
src/components/wrapper/dashboardWrapper.tsx View File

@ -22,6 +22,7 @@ import { BUILDING, GEAR } from "@/utilities/svgConstant";
import LogoutIcon from "@mui/icons-material/Logout"; import LogoutIcon from "@mui/icons-material/Logout";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { routes } from "constant/route.constant";
const drawerWidth = 260; const drawerWidth = 260;
@ -133,7 +134,7 @@ export default function DashboardWrapper(props: Props) {
const logoutHandler = () => { const logoutHandler = () => {
localStorage.removeItem("token"); localStorage.removeItem("token");
localStorage.removeItem("refreshToken"); localStorage.removeItem("refreshToken");
router.push("/log-in");
router.push(routes.LOGIN);
}; };
return ( return (


+ 2
- 1
src/hoc/authGuard/authGuard.tsx View File

@ -1,4 +1,5 @@
"use client" "use client"
import { routes } from 'constant/route.constant';
// hoc/withAuth.tsx // hoc/withAuth.tsx
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import { useEffect } from 'react'; import { useEffect } from 'react';
@ -10,7 +11,7 @@ const AuthGuard = (WrappedComponent: any) => {
useEffect(() => { useEffect(() => {
const token = localStorage.getItem('token'); const token = localStorage.getItem('token');
if (!token) { if (!token) {
router.push('/log-in');
router.push(routes.LOGIN);
} }
}, [router]); }, [router]);


+ 3
- 2
src/services/axios/axiosInstance.ts View File

@ -1,5 +1,6 @@
// axiosInstance.ts // axiosInstance.ts
import axios from 'axios'; import axios from 'axios';
import { routes } from 'constant/route.constant';
const axiosInstance = axios.create({ const axiosInstance = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL, baseURL: process.env.NEXT_PUBLIC_API_URL,
@ -55,7 +56,7 @@ axiosInstance.interceptors.response.use(
const refreshToken = localStorage.getItem('refreshToken'); const refreshToken = localStorage.getItem('refreshToken');
if (!refreshToken) { if (!refreshToken) {
window.location.href = '/log-in';
window.location.href = routes.LOGIN;
return Promise.reject(error); return Promise.reject(error);
} }
@ -74,7 +75,7 @@ axiosInstance.interceptors.response.use(
processQueue(err, null); processQueue(err, null);
localStorage.removeItem('token'); localStorage.removeItem('token');
localStorage.removeItem('refreshToken'); localStorage.removeItem('refreshToken');
window.location.href = '/login';
window.location.href = routes.LOGIN;
return Promise.reject(err); return Promise.reject(err);
} finally { } finally {
isRefreshing = false; isRefreshing = false;


Loading…
Cancel
Save