Skip to content

Commit 3d2fffd

Browse files
authored
šŸ› Add user authentication check in admin route to restrict access for non-superusers (#2145)
1 parent a8fa345 commit 3d2fffd

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

ā€Žfrontend/src/routes/_layout/admin.tsxā€Ž

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useSuspenseQuery } from "@tanstack/react-query"
2-
import { createFileRoute } from "@tanstack/react-router"
2+
import { createFileRoute, redirect } from "@tanstack/react-router"
33
import { Suspense } from "react"
44

55
import { type UserPublic, UsersService } from "@/client"
@@ -18,6 +18,14 @@ function getUsersQueryOptions() {
1818

1919
export const Route = createFileRoute("/_layout/admin")({
2020
component: Admin,
21+
beforeLoad: async () => {
22+
const user = await UsersService.readUserMe()
23+
if (!user.is_superuser) {
24+
throw redirect({
25+
to: "/",
26+
})
27+
}
28+
},
2129
head: () => ({
2230
meta: [
2331
{

0 commit comments

Comments
Ā (0)