> For the complete documentation index, see [llms.txt](https://mile-hi-labs.gitbook.io/react-session/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mile-hi-labs.gitbook.io/react-session/best-practices.md).

# Best Practices

## Protected Routes

Most apps that require authentication will have protected routes (such as the user's profile). To accomplish this, you'll need to wait for `session.loaded = true` , then you can check `session.authenticated()` to see if the user is authenticated. Assuming they are, the session object should contain everything you need to route user's appropriately.

```javascript
import React, { Fragment, useEffect } from 'react';
import { Switch, Route, Redirect, useParams, useRouteMatch } from 'react-router-dom';
import { withSession } from '@mile-hi-labs/react-session';

// Routes
import IndexRoute from 'routes/index';
import LoginRoute from 'routes/auth/login';
import RegisterRoute from 'routes/auth/register';
import UsersDetailRoute from 'routes/users/detail';

const Router = (props) => {
  const { session, store, toast } = props;

  return (	    
    <Switch>
      <Route exact path='/' render={routeProps => <IndexRoute {...props} {...routeProps}/>} />
      <Route exact path='/login' render={routeProps => <LoginRoute {...props} {...routeProps}/>} />
      <Route exact path='/register' render={routeProps => <RegisterRoute {...props} {...routeProps}/>} />
      <Route path='/users/:userId'>
        {session.loaded && (
          session.authenticated() ? <UsersDetail {...props} /> : <Redirect to='/login' />
        )}
      </Route>
    </Switch>
  )
}

export default withSession(Router);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mile-hi-labs.gitbook.io/react-session/best-practices.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
