Skip to content

Commit

Permalink
docs: Replaced deprecated <Set private> with PrivateSet within router…
Browse files Browse the repository at this point in the history
….md (#9749)

Co-authored-by: Tobbe Lundberg <[email protected]>
Co-authored-by: Dominic Saadi <[email protected]>
  • Loading branch information
3 people authored Dec 25, 2023
1 parent ffb0a99 commit 909ab4e
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions docs/docs/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,18 @@ becomes...
</MainLayout>
```

### `private` Set
### `PrivateSet`

Sets can take a `private` prop which makes all Routes inside that Set require authentication. When a user isn't authenticated and attempts to visit one of the Routes in the private Set, they'll be redirected to the Route passed as the Set's `unauthenticated` prop. The originally-requested Route's path is added to the query string as a `redirectTo` param. This lets you send the user to the page they originally requested once they're logged-in.
A `PrivateSet` makes all Routes inside that Set require authentication. When a user isn't authenticated and attempts to visit one of the Routes in the `PrivateSet`, they'll be redirected to the Route passed as the `PrivateSet`'s `unauthenticated` prop. The originally-requested Route's path is added to the query string as a `redirectTo` param. This lets you send the user to the page they originally requested once they're logged-in.

Here's an example of how you'd use a private set:

```jsx title="Routes.js"
<Router>
<Route path="/" page={HomePage} name="home" />
<Set private unauthenticated="home">
<Route path="/admin" page={AdminPage} name="admin" />
</Set>
</Router>
```

Private routes are important and should be easy to spot in your Routes file. The larger your Routes file gets, the more difficult it will probably become to find `<Set private /*...*/>` among your other Sets. So we also provide a `<PrivateSet>` component that's just an alias for `<Set private /*...*/>`. Most of our documentation uses `<PrivateSet>`.

Here's the same example again, but now using `<PrivateSet>`

```jsx title="Routes.js"
<Router>
<Route path="/" page={HomePage} name="home" />
<PrivateSet unauthenticated="home">
<Route path="/admin" page={AdminPage} name="admin" />
<PrivateSet>
</PrivateSet>
</Router>
```

Expand All @@ -164,7 +151,7 @@ To protect `Private` routes for access by a single role:
<Router>
<PrivateSet unauthenticated="forbidden" roles="admin">
<Route path="/admin/users" page={UsersPage} name="users" />
<PrivateSet>
</PrivateSet>

<Route path="/forbidden" page={ForbiddenPage} name="forbidden" />
</Router>
Expand All @@ -176,7 +163,7 @@ To protect `Private` routes for access by multiple roles:
<Router>
<PrivateSet unauthenticated="forbidden" roles={['admin', 'editor', 'publisher']}>
<Route path="/admin/posts/{id:Int}/edit" page={EditPostPage} name="editPost" />
<PrivateSet>
</PrivateSet>

<Route path="/forbidden" page={ForbiddenPage} name="forbidden" />
</Router>
Expand Down Expand Up @@ -572,7 +559,7 @@ When the lazy-loaded page is loading, `PageLoadingContext.Consumer` will pass `{

Let's say you have a dashboard area on your Redwood app, which can only be accessed after logging in. When Redwood Router renders your private page, it will first fetch the user's details, and only render the page if it determines the user is indeed logged in.

In order to display a loader while auth details are being retrieved you can add the `whileLoadingAuth` prop to your private `<Route>`, `<Set private>` or the `<PrivateSet>` component:
In order to display a loader while auth details are being retrieved you can add the `whileLoadingAuth` prop to your private `<Route>` or `<PrivateSet>` component:

```jsx
//Routes.js
Expand Down

0 comments on commit 909ab4e

Please sign in to comment.