File size: 689 Bytes
10dd572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { Response } from 'express'
import type { AccessContext } from '../access'
import { AuthenticationError } from '../shared/errors/errors'

/**
 * The Route-side accessor for what `accessMiddleware` put on the response.
 *
 * Every protected Route starts with this call, so a Route mounted without the middleware fails
 * with a 401 naming the cause instead of running on an undefined context.
 */
export function requireAccessContext(res: Response): AccessContext {
  const context = res.locals.accessContext as AccessContext | undefined

  if (!context) {
    throw new AuthenticationError('访问上下文缺失:该路由未挂载 access 中间件')
  }

  return context
}