nextjs route middleware for authentication : <p>I'm trying to figure out an appropriate way of doing authentication, which I know is a touchy subject on the <a href="https://github.com/zeit/next.js/issues/153" rel="noreferrer">GitHub issue page</a>. </p>

<p>My authentication is simple. I store a JWT token in the session. I send it to a different server for approval. If I get back true, we keep going, if I get back false, it clears the session and puts sends them to the main page.</p>

<p>In my <code>server.js</code> file I have the following (note- I am using the example from <a href="https://nextjs.org/learn/basics/create-dynamic-pages" rel="noreferrer">nextjs learn</a> and just adding <code>isAuthenticated</code>):</p>

<pre><code>function isAuthenticated(req, res, next) {
  //checks go here

  //if (req.user.authenticated)
   // return next();

  // IF A USER ISN'T LOGGED IN, THEN REDIRECT THEM SOMEWHERE
  res.redirect('/');
}

server.get('/p/:id', isAuthenticated, (req, res) =&gt; {
  const actualPage = '/post'
  const queryParams = { id: req.params.id }
  app.render(req, res, actualPage, queryParams)
})
</code></pre>

<p>This works as designed. If I refresh the page <code>/p/123</code>, it will redirect to the <code>/</code>. However, if I go there via a <code>next/link</code> href, it doesn't. Which I believe is because it's not using express at this point but next's custom routing.</p>

<p>Is there a way I can bake in a check for every single <code>next/link</code> that doesn't go through express so that I can make sure the user is logged in?</p>
