feat(auth): Implemented Session Persistence
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import { BrowserRouter as Router, Routes, Route, Navigate, useNavigate } from 'react-router-dom';
|
||||
import { useSelector } from 'react-redux';
|
||||
import React from 'react';
|
||||
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
||||
import Login from './features/auth/Login';
|
||||
import ForgotPassword from './features/auth/ForgotPassword';
|
||||
import AppLayout from './features/layouts/AppLayout';
|
||||
@@ -8,8 +7,8 @@ import AdminDashboard from './features/dashboard/AdminDashboard';
|
||||
import ClientDashboard from './features/dashboard/ClientDashboard';
|
||||
import VendorDashboard from './features/dashboard/VendorDashboard';
|
||||
import ProtectedRoute from './features/layouts/ProtectedRoute';
|
||||
import { getDashboardPath } from './services/firestoreService';
|
||||
import type { RootState } from './store/store';
|
||||
import { RoleDashboardRedirect } from './features/dashboard/RoleDashboardRedirect';
|
||||
import PublicLayout from './features/layouts/PublicLayout';
|
||||
|
||||
/**
|
||||
* AppRoutes Component
|
||||
@@ -25,7 +24,9 @@ const AppRoutes: React.FC = () => {
|
||||
<Route
|
||||
path="/login"
|
||||
element={
|
||||
<Login />
|
||||
<PublicLayout>
|
||||
<Login />
|
||||
</PublicLayout>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
@@ -80,27 +81,5 @@ const AppRoutes: React.FC = () => {
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* RoleDashboardRedirect Component
|
||||
* Dynamically redirects users to their appropriate dashboard based on their role
|
||||
*/
|
||||
const RoleDashboardRedirect: React.FC = () => {
|
||||
const { isAuthenticated, user } = useSelector((state: RootState) => state.auth);
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) {
|
||||
navigate('/login', { replace: true });
|
||||
return;
|
||||
}
|
||||
|
||||
if (user?.userRole) {
|
||||
const dashboardPath = getDashboardPath(user.userRole);
|
||||
navigate(dashboardPath, { replace: true });
|
||||
}
|
||||
}, [isAuthenticated, user?.userRole, navigate]);
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export default AppRoutes;
|
||||
|
||||
Reference in New Issue
Block a user