Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render null within PrivateRouteBase. Closes #12 #13

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/components/PrivateRoute/PrivateRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { PrivateRouteBase } from "./PrivateRouteBase";
export const PrivateRoute = async ({ children }: PropsWithChildren) => {
const user = await getServerUser();

if (!user) return null; // Prevent server side render of authorized page
// prevent server side render of authorized page
const view = !user ? null : children;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const view = !user ? null : children;
if(!user) redirect ('/login')

Wdyt? @josephdburdick @jakubczarnowski

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me but if we went in this direction what purpose would withPrivateRoute serve?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some scenarios withPrivateRoute can be faster than PrivateRoute because it's a client component (The useQuery can be reloaded before this server component is recalculated)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the client could be faster but your proposal would short-circuit at the server level before the client side ever had a chance to run.

Or am I missing something? Perhaps it only short-circuits with a redirect upon navigating but if a user's authentication expires the client side withPrivateRoute would redirect from there?


return <PrivateRouteBase>{children}</PrivateRouteBase>;
return <PrivateRouteBase>{view}</PrivateRouteBase>;
};