title: How to implement optional authentication in React/Express app?
body: <p>I'm building self-hosted bookmarks manager app. I'm using React on frontend and Express on backend. I have already implemented normal authentication system based on JWT. But now I want to make authentication optional. Beacuse it's self-hosted app some users might want to use it only on local network = auth disabled, and other expose it to the Internet = auth enabled.</p>
  <p>On backend, I'm using env variable <code>USE_AUTH=false</code> to disable authentication and everything works as it should.</p>
  <p>On frontend however, I'm rendering some components and routes based on <code>isAuthenticated</code> value stored in AuthContext state. It is set to true on successful login/sign up and to false on logout.</p>
  <p><strong>My question</strong> is how can I tell my React frontend that I'm not using auth on backend so it can render all hidden components, don't include token to requests etc.?</p>
  <p>The only solution I came up with, is to send request when user opens the app to ask server if it's using authentication. So something like <code>GET /api/auth/ping</code> will return <code>{ useAuth: true }</code> or <code>{ useAuth: false }</code> depending on <code>USE_AUTH</code> value.</p>
  <p><strong>But</strong> I don't know if it is as optimal solution. Is there a better option / pattern I can use?</p>
  <p>Thanks</p>