File tree Expand file tree Collapse file tree 8 files changed +40
-15
lines changed Expand file tree Collapse file tree 8 files changed +40
-15
lines changed Original file line number Diff line number Diff line change 1
1
import AdminUserManagement from "@/components/admin-user-management/admin-user-management" ;
2
+ import AuthPageWrapper from "@/components/auth/auth-page-wrapper" ;
2
3
3
4
export default function AdminUserManagementPage ( ) {
4
- return < AdminUserManagement /> ;
5
+ return (
6
+ < AuthPageWrapper requireAdmin >
7
+ < AdminUserManagement />
8
+ </ AuthPageWrapper >
9
+ ) ;
5
10
}
Original file line number Diff line number Diff line change
1
+ import AuthPageWrapper from "@/components/auth/auth-page-wrapper" ;
1
2
import QuestionListing from "@/components/questions/questions-listing" ;
2
3
import { Suspense } from "react" ;
3
4
4
5
export default function QuestionListingPage ( ) {
5
6
return (
6
- < Suspense >
7
- < QuestionListing />
8
- </ Suspense >
7
+ < AuthPageWrapper requireLoggedIn >
8
+ < Suspense >
9
+ < QuestionListing />
10
+ </ Suspense >
11
+ </ AuthPageWrapper >
9
12
) ;
10
13
}
Original file line number Diff line number Diff line change
1
+ import AuthPageWrapper from "@/components/auth/auth-page-wrapper" ;
1
2
import UserSettings from "@/components/user-settings/user-settings" ;
2
3
3
4
export default function UserSettingsPage ( {
4
5
params,
5
6
} : {
6
7
params : { user_id : string } ;
7
8
} ) {
8
- return < UserSettings userId = { params . user_id } /> ;
9
+ return (
10
+ < AuthPageWrapper requireLoggedIn userId = { params . user_id } >
11
+ < UserSettings userId = { params . user_id } /> ;
12
+ </ AuthPageWrapper >
13
+ ) ;
9
14
}
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import "./globals.css";
4
4
import { ThemeProvider } from "@/components/theme-provider" ;
5
5
import { Toaster } from "@/components/ui/toaster" ;
6
6
import AuthProvider from "@/app/auth/auth-context" ;
7
+ import AuthPageWrapper from "@/components/auth/auth-page-wrapper" ;
7
8
import { Navbar } from "@/components/navbar" ;
8
9
9
10
const geistSans = localFont ( {
@@ -40,7 +41,7 @@ export default function RootLayout({
40
41
>
41
42
< AuthProvider >
42
43
< Navbar />
43
- { children }
44
+ < AuthPageWrapper > { children } </ AuthPageWrapper >
44
45
</ AuthProvider >
45
46
< Toaster />
46
47
</ ThemeProvider >
Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ import {
15
15
import LoadingScreen from "@/components/common/loading-screen" ;
16
16
import AdminEditUserModal from "@/components/admin-user-management/admin-edit-user-modal" ;
17
17
import { PencilIcon , Trash2Icon } from "lucide-react" ;
18
- import AuthPageWrapper from "@/components/auth/auth-page-wrapper" ;
19
18
import { User , UserArraySchema } from "@/lib/schemas/user-schema" ;
20
19
21
20
const fetcher = async ( url : string ) : Promise < User [ ] > => {
@@ -88,7 +87,7 @@ export default function AdminUserManagement() {
88
87
} ;
89
88
90
89
return (
91
- < AuthPageWrapper requireAdmin >
90
+ < >
92
91
< div className = "container mx-auto p-4" >
93
92
< h1 className = "text-2xl font-bold mb-4" > User Management</ h1 >
94
93
< AdminEditUserModal
@@ -137,6 +136,6 @@ export default function AdminUserManagement() {
137
136
</ TableBody >
138
137
</ Table >
139
138
</ div >
140
- </ AuthPageWrapper >
139
+ </ >
141
140
) ;
142
141
}
Original file line number Diff line number Diff line change @@ -6,10 +6,13 @@ import { useAuth } from "@/app/auth/auth-context";
6
6
import { Button } from "@/components/ui/button" ;
7
7
import { useRouter } from "next/navigation" ;
8
8
9
- type AuthCheck = ( user : { isAdmin : boolean } | undefined | null ) => boolean ;
9
+ type AuthCheck = (
10
+ user : { id : string ; isAdmin : boolean } | undefined | null
11
+ ) => boolean ;
10
12
11
13
interface AuthPageWrapperProps extends React . HTMLProps < HTMLDivElement > {
12
14
children : ReactNode ;
15
+ userId ?: string ;
13
16
14
17
// User access rules
15
18
authCheck ?: AuthCheck ; // Custom predicate which is true when user is to be granted access
@@ -19,20 +22,24 @@ interface AuthPageWrapperProps extends React.HTMLProps<HTMLDivElement> {
19
22
20
23
const AuthPageWrapper : React . FC < AuthPageWrapperProps > = ( {
21
24
children,
25
+ userId,
22
26
...props
23
27
} ) => {
24
28
const auth = useAuth ( ) ;
25
29
const router = useRouter ( ) ;
26
30
27
31
const authCheck = (
28
- user : { isAdmin : boolean } | undefined | null
32
+ user : { id : string ; isAdmin : boolean } | undefined | null
29
33
) : boolean => {
30
34
if ( props ?. requireLoggedIn && ! user ) {
31
35
return false ;
32
36
}
33
37
if ( props ?. requireAdmin && ! user ?. isAdmin ) {
34
38
return false ;
35
39
}
40
+ if ( userId && user ?. id !== userId ) {
41
+ return false ;
42
+ }
36
43
if ( props ?. authCheck ) {
37
44
return props . authCheck ( user ) ;
38
45
}
@@ -53,7 +60,9 @@ const AuthPageWrapper: React.FC<AuthPageWrapperProps> = ({
53
60
< Button
54
61
size = "lg"
55
62
onClick = { ( ) => {
56
- auth ?. user ? router . push ( "/" ) : router . push ( "/auth/login" ) ;
63
+ auth ?. user
64
+ ? router . push ( "/app/questions" )
65
+ : router . push ( "/auth/login" ) ;
57
66
} }
58
67
>
59
68
Return Home
Original file line number Diff line number Diff line change @@ -22,6 +22,10 @@ export function Navbar() {
22
22
23
23
const isActive = ( path : string ) => pathname === path ;
24
24
25
+ if ( ! auth ?. user ) {
26
+ return ;
27
+ }
28
+
25
29
return (
26
30
< nav className = "bg-background border-b" >
27
31
< div className = "max-w-7xl mx-auto px-4 sm:px-6 lg:px-8" >
Original file line number Diff line number Diff line change @@ -23,7 +23,6 @@ import ProfileTab from "@/components/user-settings/profile-tab";
23
23
import LoadingScreen from "@/components/common/loading-screen" ;
24
24
import { useAuth } from "@/app/auth/auth-context" ;
25
25
import { cn } from "@/lib/utils" ;
26
- import AuthPageWrapper from "../auth/auth-page-wrapper" ;
27
26
import { User , UserSchema } from "@/lib/schemas/user-schema" ;
28
27
29
28
const fetcher = async ( url : string ) : Promise < User > => {
@@ -309,7 +308,7 @@ export default function UserSettings({ userId }: { userId: string }) {
309
308
}
310
309
311
310
return (
312
- < AuthPageWrapper requireLoggedIn >
311
+ < >
313
312
{ error ? (
314
313
< div > Error: Failed to load user data</ div >
315
314
) : ! user ? (
@@ -496,6 +495,6 @@ export default function UserSettings({ userId }: { userId: string }) {
496
495
</ Tabs >
497
496
</ div >
498
497
) }
499
- </ AuthPageWrapper >
498
+ </ >
500
499
) ;
501
500
}
You can’t perform that action at this time.
0 commit comments