Skip to content

Commit 0dc5b1c

Browse files
committed
Edit user settings user access
1 parent 6d9acb5 commit 0dc5b1c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

frontend/app/app/user-settings/[user_id]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function UserSettingsPage({
77
params: { user_id: string };
88
}) {
99
return (
10-
<AuthPageWrapper requireLoggedIn>
10+
<AuthPageWrapper requireLoggedIn userId={params.user_id}>
1111
<UserSettings userId={params.user_id} />;
1212
</AuthPageWrapper>
1313
);

frontend/components/auth/auth-page-wrapper.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { useAuth } from "@/app/auth/auth-context";
66
import { Button } from "@/components/ui/button";
77
import { useRouter } from "next/navigation";
88

9-
type AuthCheck = (user: { isAdmin: boolean } | undefined | null) => boolean;
9+
type AuthCheck = (
10+
user: { id: string; isAdmin: boolean } | undefined | null
11+
) => boolean;
1012

1113
interface AuthPageWrapperProps extends React.HTMLProps<HTMLDivElement> {
1214
children: ReactNode;
15+
userId?: string;
1316

1417
// User access rules
1518
authCheck?: AuthCheck; // Custom predicate which is true when user is to be granted access
@@ -19,20 +22,24 @@ interface AuthPageWrapperProps extends React.HTMLProps<HTMLDivElement> {
1922

2023
const AuthPageWrapper: React.FC<AuthPageWrapperProps> = ({
2124
children,
25+
userId,
2226
...props
2327
}) => {
2428
const auth = useAuth();
2529
const router = useRouter();
2630

2731
const authCheck = (
28-
user: { isAdmin: boolean } | undefined | null
32+
user: { id: string; isAdmin: boolean } | undefined | null
2933
): boolean => {
3034
if (props?.requireLoggedIn && !user) {
3135
return false;
3236
}
3337
if (props?.requireAdmin && !user?.isAdmin) {
3438
return false;
3539
}
40+
if (userId && user?.id !== userId) {
41+
return false;
42+
}
3643
if (props?.authCheck) {
3744
return props.authCheck(user);
3845
}
@@ -53,7 +60,9 @@ const AuthPageWrapper: React.FC<AuthPageWrapperProps> = ({
5360
<Button
5461
size="lg"
5562
onClick={() => {
56-
auth?.user ? router.push("/") : router.push("/auth/login");
63+
auth?.user
64+
? router.push("/app/questions")
65+
: router.push("/auth/login");
5766
}}
5867
>
5968
Return Home

0 commit comments

Comments
 (0)