Skip to content

Commit 91fdaee

Browse files
committed
added: Admin Routes, authenicated it and it only authorize admin only
1 parent 013de13 commit 91fdaee

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

client/src/routeTree.gen.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import { Route as ContactRouteImport } from './routes/contact'
1313
import { Route as AboutRouteImport } from './routes/about'
1414
import { Route as IndexRouteImport } from './routes/index'
1515
import { Route as TrackIndexRouteImport } from './routes/track/index'
16+
import { Route as OwnerIndexRouteImport } from './routes/owner/index'
1617
import { Route as BlogsIndexRouteImport } from './routes/blogs/index'
1718
import { Route as TrackCodeRouteImport } from './routes/track/$code'
19+
import { Route as OwnerLoginRouteImport } from './routes/owner/login'
20+
import { Route as OwnerDashboardRouteImport } from './routes/owner/dashboard'
1821
import { Route as BlogsIdRouteImport } from './routes/blogs/$id'
1922

2023
const ContactRoute = ContactRouteImport.update({
@@ -37,6 +40,11 @@ const TrackIndexRoute = TrackIndexRouteImport.update({
3740
path: '/track/',
3841
getParentRoute: () => rootRouteImport,
3942
} as any)
43+
const OwnerIndexRoute = OwnerIndexRouteImport.update({
44+
id: '/owner/',
45+
path: '/owner/',
46+
getParentRoute: () => rootRouteImport,
47+
} as any)
4048
const BlogsIndexRoute = BlogsIndexRouteImport.update({
4149
id: '/blogs/',
4250
path: '/blogs/',
@@ -47,6 +55,16 @@ const TrackCodeRoute = TrackCodeRouteImport.update({
4755
path: '/track/$code',
4856
getParentRoute: () => rootRouteImport,
4957
} as any)
58+
const OwnerLoginRoute = OwnerLoginRouteImport.update({
59+
id: '/owner/login',
60+
path: '/owner/login',
61+
getParentRoute: () => rootRouteImport,
62+
} as any)
63+
const OwnerDashboardRoute = OwnerDashboardRouteImport.update({
64+
id: '/owner/dashboard',
65+
path: '/owner/dashboard',
66+
getParentRoute: () => rootRouteImport,
67+
} as any)
5068
const BlogsIdRoute = BlogsIdRouteImport.update({
5169
id: '/blogs/$id',
5270
path: '/blogs/$id',
@@ -58,17 +76,23 @@ export interface FileRoutesByFullPath {
5876
'/about': typeof AboutRoute
5977
'/contact': typeof ContactRoute
6078
'/blogs/$id': typeof BlogsIdRoute
79+
'/owner/dashboard': typeof OwnerDashboardRoute
80+
'/owner/login': typeof OwnerLoginRoute
6181
'/track/$code': typeof TrackCodeRoute
6282
'/blogs': typeof BlogsIndexRoute
83+
'/owner': typeof OwnerIndexRoute
6384
'/track': typeof TrackIndexRoute
6485
}
6586
export interface FileRoutesByTo {
6687
'/': typeof IndexRoute
6788
'/about': typeof AboutRoute
6889
'/contact': typeof ContactRoute
6990
'/blogs/$id': typeof BlogsIdRoute
91+
'/owner/dashboard': typeof OwnerDashboardRoute
92+
'/owner/login': typeof OwnerLoginRoute
7093
'/track/$code': typeof TrackCodeRoute
7194
'/blogs': typeof BlogsIndexRoute
95+
'/owner': typeof OwnerIndexRoute
7296
'/track': typeof TrackIndexRoute
7397
}
7498
export interface FileRoutesById {
@@ -77,8 +101,11 @@ export interface FileRoutesById {
77101
'/about': typeof AboutRoute
78102
'/contact': typeof ContactRoute
79103
'/blogs/$id': typeof BlogsIdRoute
104+
'/owner/dashboard': typeof OwnerDashboardRoute
105+
'/owner/login': typeof OwnerLoginRoute
80106
'/track/$code': typeof TrackCodeRoute
81107
'/blogs/': typeof BlogsIndexRoute
108+
'/owner/': typeof OwnerIndexRoute
82109
'/track/': typeof TrackIndexRoute
83110
}
84111
export interface FileRouteTypes {
@@ -88,26 +115,35 @@ export interface FileRouteTypes {
88115
| '/about'
89116
| '/contact'
90117
| '/blogs/$id'
118+
| '/owner/dashboard'
119+
| '/owner/login'
91120
| '/track/$code'
92121
| '/blogs'
122+
| '/owner'
93123
| '/track'
94124
fileRoutesByTo: FileRoutesByTo
95125
to:
96126
| '/'
97127
| '/about'
98128
| '/contact'
99129
| '/blogs/$id'
130+
| '/owner/dashboard'
131+
| '/owner/login'
100132
| '/track/$code'
101133
| '/blogs'
134+
| '/owner'
102135
| '/track'
103136
id:
104137
| '__root__'
105138
| '/'
106139
| '/about'
107140
| '/contact'
108141
| '/blogs/$id'
142+
| '/owner/dashboard'
143+
| '/owner/login'
109144
| '/track/$code'
110145
| '/blogs/'
146+
| '/owner/'
111147
| '/track/'
112148
fileRoutesById: FileRoutesById
113149
}
@@ -116,8 +152,11 @@ export interface RootRouteChildren {
116152
AboutRoute: typeof AboutRoute
117153
ContactRoute: typeof ContactRoute
118154
BlogsIdRoute: typeof BlogsIdRoute
155+
OwnerDashboardRoute: typeof OwnerDashboardRoute
156+
OwnerLoginRoute: typeof OwnerLoginRoute
119157
TrackCodeRoute: typeof TrackCodeRoute
120158
BlogsIndexRoute: typeof BlogsIndexRoute
159+
OwnerIndexRoute: typeof OwnerIndexRoute
121160
TrackIndexRoute: typeof TrackIndexRoute
122161
}
123162

@@ -151,6 +190,13 @@ declare module '@tanstack/react-router' {
151190
preLoaderRoute: typeof TrackIndexRouteImport
152191
parentRoute: typeof rootRouteImport
153192
}
193+
'/owner/': {
194+
id: '/owner/'
195+
path: '/owner'
196+
fullPath: '/owner'
197+
preLoaderRoute: typeof OwnerIndexRouteImport
198+
parentRoute: typeof rootRouteImport
199+
}
154200
'/blogs/': {
155201
id: '/blogs/'
156202
path: '/blogs'
@@ -165,6 +211,20 @@ declare module '@tanstack/react-router' {
165211
preLoaderRoute: typeof TrackCodeRouteImport
166212
parentRoute: typeof rootRouteImport
167213
}
214+
'/owner/login': {
215+
id: '/owner/login'
216+
path: '/owner/login'
217+
fullPath: '/owner/login'
218+
preLoaderRoute: typeof OwnerLoginRouteImport
219+
parentRoute: typeof rootRouteImport
220+
}
221+
'/owner/dashboard': {
222+
id: '/owner/dashboard'
223+
path: '/owner/dashboard'
224+
fullPath: '/owner/dashboard'
225+
preLoaderRoute: typeof OwnerDashboardRouteImport
226+
parentRoute: typeof rootRouteImport
227+
}
168228
'/blogs/$id': {
169229
id: '/blogs/$id'
170230
path: '/blogs/$id'
@@ -180,8 +240,11 @@ const rootRouteChildren: RootRouteChildren = {
180240
AboutRoute: AboutRoute,
181241
ContactRoute: ContactRoute,
182242
BlogsIdRoute: BlogsIdRoute,
243+
OwnerDashboardRoute: OwnerDashboardRoute,
244+
OwnerLoginRoute: OwnerLoginRoute,
183245
TrackCodeRoute: TrackCodeRoute,
184246
BlogsIndexRoute: BlogsIndexRoute,
247+
OwnerIndexRoute: OwnerIndexRoute,
185248
TrackIndexRoute: TrackIndexRoute,
186249
}
187250
export const routeTree = rootRouteImport
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// src/routes/owner/dashboard.tsx
2+
import { createFileRoute, redirect } from "@tanstack/react-router";
3+
4+
export const Route = createFileRoute("/owner/dashboard")({
5+
beforeLoad: async () => {
6+
const token = localStorage.getItem("adminToken");
7+
const role = localStorage.getItem("adminRole");
8+
if (!token || role !== "owner") {
9+
throw redirect({ to: "/owner/login" });
10+
}
11+
},
12+
component: Dashboard,
13+
});
14+
15+
function Dashboard() {
16+
return <h1>Admin Dashboard</h1>;
17+
}

client/src/routes/owner/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// src/routes/owner/index.tsx
2+
import { Outlet, createFileRoute } from "@tanstack/react-router";
3+
4+
export const Route = createFileRoute("/owner/")({
5+
component: () => (
6+
<div className="admin-layout">
7+
<h1>Admin Panel</h1>
8+
<Outlet />
9+
</div>
10+
),
11+
});

client/src/routes/owner/login.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// src/routes/owner/login.tsx
2+
import { useNavigate, createFileRoute } from "@tanstack/react-router";
3+
import { useState } from "react";
4+
import axios from "axios";
5+
6+
export const Route = createFileRoute("/owner/login")({
7+
component: AdminLogin,
8+
});
9+
10+
function AdminLogin() {
11+
const navigate = useNavigate();
12+
const [email, setEmail] = useState("");
13+
const [password, setPassword] = useState("");
14+
15+
const handleLogin = async () => {
16+
console.log("Email: ", email, "Password: ", password);
17+
try {
18+
const res = await axios.post("http://localhost:5000/api/auth/login", {
19+
email,
20+
password,
21+
});
22+
23+
if (res.status === 200 && res.data?.token) {
24+
localStorage.setItem("adminToken", res.data.token);
25+
localStorage.setItem("adminRole", "owner");
26+
navigate({ to: "/owner/dashboard" });
27+
} else {
28+
alert("Invalid credentials");
29+
}
30+
} catch (error: any) {
31+
console.error("Login failed:", error);
32+
alert(error.response?.data?.message || "Login failed, please try again.");
33+
}
34+
};
35+
36+
return (
37+
<div>
38+
<h2>Admin Login</h2>
39+
<input
40+
value={email}
41+
onChange={(e) => setEmail(e.target.value)}
42+
placeholder="Email"
43+
/>
44+
<input
45+
type="password"
46+
value={password}
47+
onChange={(e) => setPassword(e.target.value)}
48+
placeholder="Password"
49+
/>
50+
<button onClick={handleLogin}>Login</button>
51+
</div>
52+
);
53+
}

0 commit comments

Comments
 (0)