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

Conversation

josephdburdick
Copy link
Contributor

Motivation and context

This fixes the issue described in issue #12.

Description

With this change the server renders nothing for unauthenticated users on private routes but the client will also be able to act on this scenario, redirecting them as expected.

Steps to reproduce the behavior

Before:

Navigate to a private route without being authenticated and you will view a blank page.

After:

Navigate to a private route without being authenticated and you will view a blank page before being redirected by the useEffect hook within withPrivateRoute.

@@ -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?

@josephdburdick
Copy link
Contributor Author

Closing this PR in favor for #14 with suggested fix and appropriate branch name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants