Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit a5d1fc2

Browse files
committed
stage-test
1 parent 09afc48 commit a5d1fc2

File tree

6 files changed

+101
-56
lines changed

6 files changed

+101
-56
lines changed

.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
VITE_BACKEND_URL=https://api.beejho.in
2-
VITE_ORDER_WEBSOCKET_URL=wss://ms4ld443q8.execute-api.ap-south-1.amazonaws.com
1+
VITE_BACKEND_URL=https://stg-api.beejho.in

src/components/admin/common/RestaurantLogin.tsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,34 +155,32 @@ export default function RestaurantLogin({
155155
};
156156
});
157157
await axiosInstance
158-
.post('/v0/restaurantLogin', {
159-
userId: state.userId,
160-
password: state.password,
161-
restaurantId,
162-
})
163-
.then(async (loginResult) => {
158+
.post(
159+
'/v0/restaurantLogin',
160+
{
161+
userId: state.userId,
162+
password: state.password,
163+
restaurantId,
164+
},
165+
{ withCredentials: true },
166+
) // Ensure cookies are sent
167+
.then(() => {
164168
setShowLogin(false);
165-
localStorage.setItem('jwt', loginResult.data.jwt);
166-
axiosInstance.defaults.headers['authorization'] = `Bearer ${loginResult.data.jwt}`;
167169
if (succesfullLoginCallback) succesfullLoginCallback();
168170
})
169171
.catch((e) => {
170172
console.log(e);
171-
setState((prevState) => {
172-
return {
173-
...prevState,
174-
authError: e.response.data?.message,
175-
};
176-
});
173+
setState((prevState) => ({
174+
...prevState,
175+
authError: e.response.data?.message || 'Login failed',
176+
}));
177177
if (failedLoginCallback) failedLoginCallback();
178178
})
179179
.finally(() => {
180-
setState((prevState) => {
181-
return {
182-
...prevState,
183-
isLoading: false,
184-
};
185-
});
180+
setState((prevState) => ({
181+
...prevState,
182+
isLoading: false,
183+
}));
186184
});
187185
}}
188186
>

src/contexts/AuthContext.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createContext, useContext, useEffect, useState } from 'react';
2+
import axiosInstance from '../utils/axios';
3+
import { useParams } from 'react-router-dom';
4+
// import { useNavigate } from 'react-router-dom';
5+
6+
interface AuthContextType {
7+
isAuthenticated: boolean;
8+
checkAuth: () => Promise<void>;
9+
isLoading: boolean;
10+
// logout: () => void;
11+
}
12+
13+
const AuthContext = createContext<AuthContextType | null>(null);
14+
15+
export function AuthProvider({ children }: { children: React.ReactNode }) {
16+
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
17+
const [isLoading, setIsLoading] = useState(true);
18+
// const navigate = useNavigate();
19+
20+
// Function to check authentication status by verifying cookie
21+
const { restaurantId } = useParams();
22+
const checkAuth = async () => {
23+
try {
24+
await axiosInstance.get(`/v0/restaurant/${restaurantId}/getRestaurantOrderDetails`);
25+
setIsAuthenticated(true);
26+
} catch {
27+
setIsAuthenticated(false);
28+
} finally {
29+
setIsLoading(false);
30+
}
31+
};
32+
33+
// // Logout function to clear the cookie
34+
// const logout = async () => {
35+
// await axiosInstance.post('/v0/logout', {}, { withCredentials: true });
36+
// setIsAuthenticated(false);
37+
// w('/login'); // Redirect to login page
38+
// };
39+
40+
// Check authentication on mount
41+
useEffect(() => {
42+
checkAuth();
43+
}, []);
44+
45+
return <AuthContext.Provider value={{ isAuthenticated, checkAuth, isLoading }}>{children}</AuthContext.Provider>;
46+
}
47+
48+
// Custom hook to use authentication
49+
export function useAuth() {
50+
const context = useContext(AuthContext);
51+
if (!context) {
52+
throw new Error('useAuth must be used within an AuthProvider');
53+
}
54+
return context;
55+
}

src/contexts/UserContext.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { MenuProvider } from './contexts/CartContext';
1313
import UserOrders from './routes/UserOrders';
1414
import Dashboard from './routes/Dashboard';
1515
import RestaurantOrders from './routes/RestaurantOrders';
16+
import ProtectedRoute from './routes/ProtectedRoutes';
17+
import { AuthProvider } from './contexts/AuthContext';
18+
import RestaurantLogin from './components/admin/common/RestaurantLogin';
1619

1720
const router = createBrowserRouter([
1821
{
@@ -28,20 +31,17 @@ const router = createBrowserRouter([
2831
element: <UserOrders />,
2932
},
3033
{
31-
path: '/:restaurantId/admin/dashboard',
32-
element: <Dashboard />,
34+
path: '/:restaurantId/admin',
35+
element: <ProtectedRoute />,
36+
children: [
37+
{ path: 'dashboard', element: <Dashboard /> },
38+
{ path: 'editMenu', element: <Menu admin /> },
39+
{ path: 'orders', element: <RestaurantOrders /> },
40+
],
3341
},
34-
// {
35-
// path: '/:restaurantId/trending',
36-
// element: <Trending />,
37-
// },
3842
{
39-
path: '/:restaurantId/admin/editMenu',
40-
element: <Menu admin />,
41-
},
42-
{
43-
path: '/:restaurantId/admin/orders',
44-
element: <RestaurantOrders />,
43+
path: '/login',
44+
element: <RestaurantLogin showLogin={true} setShowLogin={() => {}} />,
4545
},
4646
{
4747
path: '*',
@@ -64,7 +64,9 @@ createRoot(document.getElementById('root')!).render(
6464
<StrictMode>
6565
<ThemeProvider theme={theme}>
6666
<MenuProvider>
67-
<RouterProvider router={router} />
67+
<AuthProvider>
68+
<RouterProvider router={router} />
69+
</AuthProvider>
6870
</MenuProvider>
6971
</ThemeProvider>
7072
</StrictMode>,

src/routes/ProtectedRoutes.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Navigate, Outlet } from 'react-router-dom';
2+
import { useAuth } from '../contexts/AuthContext';
3+
4+
export default function ProtectedRoute() {
5+
const auth = useAuth();
6+
console.log('auth', auth);
7+
if (!auth?.isLoading && auth?.isAuthenticated === false) {
8+
return <Navigate to='/login' replace />;
9+
}
10+
11+
return <Outlet />;
12+
}

0 commit comments

Comments
 (0)