Skip to content

Commit 3eba9eb

Browse files
authored
fix(fe): prevent invalid routing (#205)
* feat: update routing logic to use redirects and improve error handling * feat: refactor Button component to use forwardRef for improved ref handling * fix: remove async from beforeLoad in my route for improved performance
1 parent 7adae75 commit 3eba9eb

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

apps/client/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"plugins": ["react-hooks", "react-refresh"],
2929
"rules": {
30+
"@typescript-eslint/no-throw-literal": "off",
3031
"react/react-in-jsx-scope": "off",
3132
"react/require-default-props": "off",
3233
"react-hooks/rules-of-hooks": "error",
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { HTMLAttributes, PropsWithChildren, Ref } from 'react';
1+
import { forwardRef, HTMLAttributes, PropsWithChildren } from 'react';
2+
3+
type ButtonProps = PropsWithChildren<HTMLAttributes<HTMLButtonElement>>;
4+
5+
const Button = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
6+
const { children, className, onClick } = props;
27

3-
function Button({
4-
ref,
5-
children,
6-
className,
7-
onClick,
8-
}: PropsWithChildren<HTMLAttributes<HTMLButtonElement>> & {
9-
ref?: Ref<HTMLButtonElement>;
10-
}) {
118
return (
129
<button
1310
ref={ref}
@@ -18,6 +15,8 @@ function Button({
1815
{children}
1916
</button>
2017
);
21-
}
18+
});
19+
20+
Button.displayName = 'Button';
2221

2322
export default Button;

apps/client/src/routes/my.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createFileRoute } from '@tanstack/react-router';
1+
import { createFileRoute, redirect } from '@tanstack/react-router';
22

33
import { refresh, useAuthStore } from '@/features/auth';
44

@@ -8,14 +8,16 @@ export const Route = createFileRoute('/my')({
88
component: MyPage,
99
beforeLoad: () => {
1010
const { isLogin, setAuthInformation } = useAuthStore.getState();
11-
if (!isLogin())
12-
refresh()
11+
12+
if (!isLogin()) {
13+
return refresh()
1314
.then((res) => {
1415
setAuthInformation(res);
1516
})
1617
.catch(() => {
17-
// eslint-disable-next-line @typescript-eslint/no-throw-literal
18-
window.location.href = '/';
18+
throw redirect({ to: '/' });
1919
});
20+
}
21+
return Promise.resolve();
2022
},
2123
});

apps/client/src/routes/session/$sessionId/$questionId/index.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createFileRoute } from '@tanstack/react-router';
1+
import { createFileRoute, isRedirect, redirect } from '@tanstack/react-router';
22

33
import { refresh, useAuthStore } from '@/features/auth';
44
import { getSessionToken, useSessionStore } from '@/features/session';
@@ -52,8 +52,17 @@ export const Route = createFileRoute('/session/$sessionId/$questionId/')({
5252
response.questions.forEach((question) => {
5353
addQuestion(question);
5454
});
55+
56+
if (
57+
response.questions.every(
58+
(question) => question.questionId !== Number(questionId),
59+
)
60+
) {
61+
throw redirect({ to: `/session/${sessionId}` });
62+
}
5563
} catch (e) {
56-
window.location.href = '/';
64+
if (isRedirect(e)) throw e;
65+
throw redirect({ to: '/' });
5766
}
5867
},
5968
});

apps/client/src/routes/session/$sessionId/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createFileRoute } from '@tanstack/react-router';
1+
import { createFileRoute, redirect } from '@tanstack/react-router';
22

33
import { refresh, useAuthStore } from '@/features/auth';
44
import { getSessionToken, useSessionStore } from '@/features/session';
@@ -51,7 +51,7 @@ export const Route = createFileRoute('/session/$sessionId/')({
5151
addQuestion(question);
5252
});
5353
} catch (e) {
54-
window.location.href = '/';
54+
throw redirect({ to: '/session' });
5555
}
5656
},
5757
});

apps/client/src/routes/session/route.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
createFileRoute,
33
Outlet,
4+
redirect,
45
ScrollRestoration,
56
} from '@tanstack/react-router';
67

@@ -10,7 +11,7 @@ import { ChattingList } from '@/components';
1011

1112
export const Route = createFileRoute('/session')({
1213
beforeLoad: ({ location }) => {
13-
if (location.pathname === '/session') window.location.href = '/';
14+
if (location.pathname === '/session') throw redirect({ to: '/' });
1415
},
1516
component: () => (
1617
<div className='flex h-full w-full items-center justify-center gap-4 px-4 py-4 md:max-w-[1194px]'>

0 commit comments

Comments
 (0)