From 684c7c103326c701e3c2b6713837135c1c9f149c Mon Sep 17 00:00:00 2001
From: Matt Brophy
Date: Wed, 12 Jul 2023 17:17:33 -0400
Subject: [PATCH 1/3] Add auth example using RouterProvider
---
examples/auth-router-provider/.gitignore | 5 +
examples/auth-router-provider/.stackblitzrc | 4 +
examples/auth-router-provider/README.md | 28 +
examples/auth-router-provider/index.html | 12 +
.../auth-router-provider/package-lock.json | 1481 +++++++++++++++++
examples/auth-router-provider/package.json | 24 +
examples/auth-router-provider/src/App.tsx | 189 +++
examples/auth-router-provider/src/auth.ts | 24 +
examples/auth-router-provider/src/index.css | 12 +
examples/auth-router-provider/src/main.tsx | 12 +
.../auth-router-provider/src/vite-env.d.ts | 1 +
examples/auth-router-provider/tsconfig.json | 21 +
examples/auth-router-provider/vite.config.ts | 39 +
13 files changed, 1852 insertions(+)
create mode 100644 examples/auth-router-provider/.gitignore
create mode 100644 examples/auth-router-provider/.stackblitzrc
create mode 100644 examples/auth-router-provider/README.md
create mode 100644 examples/auth-router-provider/index.html
create mode 100644 examples/auth-router-provider/package-lock.json
create mode 100644 examples/auth-router-provider/package.json
create mode 100644 examples/auth-router-provider/src/App.tsx
create mode 100644 examples/auth-router-provider/src/auth.ts
create mode 100644 examples/auth-router-provider/src/index.css
create mode 100644 examples/auth-router-provider/src/main.tsx
create mode 100644 examples/auth-router-provider/src/vite-env.d.ts
create mode 100644 examples/auth-router-provider/tsconfig.json
create mode 100644 examples/auth-router-provider/vite.config.ts
diff --git a/examples/auth-router-provider/.gitignore b/examples/auth-router-provider/.gitignore
new file mode 100644
index 0000000000..d451ff16c1
--- /dev/null
+++ b/examples/auth-router-provider/.gitignore
@@ -0,0 +1,5 @@
+node_modules
+.DS_Store
+dist
+dist-ssr
+*.local
diff --git a/examples/auth-router-provider/.stackblitzrc b/examples/auth-router-provider/.stackblitzrc
new file mode 100644
index 0000000000..d98146f4d0
--- /dev/null
+++ b/examples/auth-router-provider/.stackblitzrc
@@ -0,0 +1,4 @@
+{
+ "installDependencies": true,
+ "startCommand": "npm run dev"
+}
diff --git a/examples/auth-router-provider/README.md b/examples/auth-router-provider/README.md
new file mode 100644
index 0000000000..f18f905f1e
--- /dev/null
+++ b/examples/auth-router-provider/README.md
@@ -0,0 +1,28 @@
+---
+title: Authentication (using RouterProvider)
+toc: false
+---
+
+# Auth Example (using RouterProvider)
+
+This example demonstrates how to restrict access to routes to authenticated users when using ``.
+
+The primary difference compared to how authentication was handled in `BrowserRouter` is that since `RouterProvider` decouples fetching from rendering, we can no longer rely on React context and/or hooks to get our user authentication status. We need access to this information outside of the React tree so we can use it in our route `loader` and `action` functions.
+
+For some background information on this design choice, please check out the [Remixing React Router](https://remix.run/blog/remixing-react-router) blog post and Ryan's [When to Fetch](https://www.youtube.com/watch?v=95B8mnhzoCM) talk from Reactathon.
+
+Be sure to pay attention to the following features in this example:
+
+- The use of a standalone object _outside of the React tree_ that manages our authentication state
+- The use of `loader` functions to check for user authentication
+- The use of `redirect` from the `/protexted` `loader` when the user is not logged in
+- The use of a `
} />
+ );
+}
+
+function Layout() {
+ return (
+
+
Auth Example using RouterProvider
+
+
+ This example demonstrates a simple login flow with three pages: a public
+ page, a protected page, and a login page. In order to see the protected
+ page, you must first login. Pretty standard stuff.
+
+
+
+ First, visit the public page. Then, visit the protected page. You're not
+ yet logged in, so you are redirected to the login page. After you login,
+ you are redirected back to the protected page.
+
+
+
+ Notice the URL change each time. If you click the back button at this
+ point, would you expect to go back to the login page? No! You're already
+ logged in. Try it out, and you'll see you go back to the page you
+ visited just *before* logging in, the public page.
+
+
+
+
+
+
+ Public Page
+
+
+ Protected Page
+
+
+
+
+
+ );
+}
+
+function AuthStatus() {
+ let { user } = useRouteLoaderData("root") as { user: string | null };
+ let fetcher = useFetcher();
+
+ if (!user) {
+ return