Authentication

We can add information about the authenticated user to pageContext at the server middlware:

const renderPage = createPageRenderer(/* ... */)

// `vite-plugin-ssr` server middleware
app.get('*', async (req, res) => {
  // Authentication middlewares (e.g. Passport.js or Grant) provide information
  // about the logged-in user on the `req` object.
  const user = req.user

  /* Or when using a third-party authentication provider (e.g. Auth0):
  const user = await authProviderApi.getUser(req.headers)
  */

  const pageContext = {
    url: req.url,
    // We make logged-in user information available to pages as `pageContext.user`
    user
  }
  const result = await renderPage(pageContext)

  /* ... */
})

Common auth tools:

In principle, we should be able to use vite-plugin-ssr with any authentication tool. However, if you are having difficulties integrating an authentication tool, feel free to join and ask our Discord or open a GitHub ticket.

Edit this page