guard() hook

The guard() hook enables you to protect pages from unauthorized access, see Guides > Authentication > Login flow.

Note that:

export { guard }
 
import { render } from 'vike/abort'
 
async function guard(pageContext) {
  if (pageContext.urlPathname === '/hello/forbidden') {
    throw render(401, 'This page is forbidden.')
  }
}

TypeScript

export { guard }
 
import type { GuardAsync } from 'vike/types'
import { render } from 'vike/abort'
 
const guard: GuardAsync = async (pageContext): ReturnType<GuardAsync> => {
  // ...
}