Replies: 4 comments 9 replies
-
What you need to do is to branch your code in both loader and UI. export async function loader({ request }: DataFunctionArgs) {
let isAuthenticated = getAuthenticatedUser(request);
if (isAuthenticated) {
// run code to get user home page data and return a response
}
// run code to get the public data and return a response
}
export default function Component() {
let loaderData = useLoaderData<typeof loader>()
if (loaderData.type === "home") return <Home />
return <Login />
} If you're using React 18, you could use React.lazy and dynamic imports to import Home and Login components, this way if the user is authenticated it will not download the JS for Login and if it's not authenticated it will not download the JS for Home. If you don't add a Suspense boundary around those component React will render the Home or Login UI on SSR. |
Beta Was this translation helpful? Give feedback.
-
@sergiodxa how do i share import type { LinksFunction, LoaderFunctionArgs } from "@remix-run/node"
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
useLoaderData,
} from "@remix-run/react"
import styles from "./styles/tailwind.css"
export const links: LinksFunction = () => [
{
rel: "stylesheet",
href: styles,
},
]
export const loader = async ({ request }: LoaderFunctionArgs) => {
const isAuthenticated = false // getAuthenticatedUser(request)
if (isAuthenticated) {
// run code to get user home page data and return a response
return {
type: "dashboard",
}
}
// run code to get the public data and return a response
return {
type: "signup",
}
}
const SignUp = () => (
<>
<h1>SignUp</h1>
</>
)
const Dashboard = () => (
<>
<h1>Dashboard</h1>
</>
)
const App = () => {
const loaderData = useLoaderData<typeof loader>()
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
{loaderData.type === "signup" ? <SignUp /> : <Dashboard />}
<Outlet />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
)
}
export default App |
Beta Was this translation helpful? Give feedback.
-
This might help: https://stackoverflow.com/a/77846674/9543932 |
Beta Was this translation helpful? Give feedback.
-
for anyone in future looking for a copy-paste answer, this is how you do it. edit: its way too complicated when you add authentication & sessions to it so i went with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now, all of the Remix examples I can find of authenticated routes start with the assumption that you'll redirect unauthenticated users away from a given route, like
/
, to a login route, like/login
. It's much more common that a website will support unauthenticated users visiting/
. If you go to https://github.com/ unauthenticated, you get the marketing website. If you're authenticated, you get your feed.It's not obvious so far how to do this in Remix. Maybe I could fetch the user's logged-in state in a loader and then render one route or another, but then we run into the issue of layouts - the authenticated version of the page probably wants to have a layout route with the user's avatar and stuff on it. The unauthenticated version doesn't. How do you render "another route" from a route in Remix? So far I haven't found docs for that.
So: what kind of patterns might work for rendering both a "marketing site" and a "logged in dashboard" at
/
, with Remix?Any help much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions