server/controllers/authController.js
/** Check if the user type has access to the route */
Check if the user type has access to the route
Check if the user type has access to the route
function checkAccess(route, type, isServerRoute) {
  let input = {}
  input.route = route
  input.type = type
  const [vErr, v] = validate('route, type', input, authSchema)
  if (vErr || !v) return false
  function wilcdardMatch(str, accessRule) {
    const escapeRegex = (str) =>
      str.replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1')
    return new RegExp(
      '^' + accessRule.split('*').map(escapeRegex).join('.*') + '$'
    ).test(str)
  }
  const accessList = isServerRoute ? serverList : clientList
  let result = 'noRouteMatch'
  for (const accessRule of accessList) {
    if (wilcdardMatch(route, accessRule[0])) {
      if (accessRule[1] === '') {
        result = 'allMatch'
        break
      }
      if (accessRule[1].includes(type)) {
        result = 'typeMatch'
        break
      }
      result = 'noTypeMatch'
      break
    }
  }
  if (result === 'noRouteMatch') {
    consola.info(`No route match. Route: ${route}`)
  }
  return result
}
function checkAccess ( route , type , isServerRoute ) { let input = { } input . route = route input . type = type const [ vErr , v ] = validate ( 'route, type' , input , authSchema ) if ( vErr || ! v ) return false function wilcdardMatch ( str , accessRule ) { const escapeRegex = ( str ) => str . replace ( / ([.*+?^=!:${}()|[\]/\\]) / g , '\\$1' ) return new RegExp ( '^' + accessRule . split ( '*' ) . map ( escapeRegex ) . join ( '.*' ) + '$' ) . test ( str ) } const accessList = isServerRoute ? serverList : clientList let result = 'noRouteMatch' for ( const accessRule of accessList ) { if ( wilcdardMatch ( route , accessRule [ 0 ] ) ) { if ( accessRule [ 1 ] === '' ) { result = 'allMatch' break } if ( accessRule [ 1 ] . includes ( type ) ) { result = 'typeMatch' break } result = 'noTypeMatch' break } } if ( result === 'noRouteMatch' ) { consola . info ( ` ${ route } ` ) } return result }
Check if the user type has access to the route
Check if the user type has access to the route
/** --- Validate input */ // Prepare input for validation /** Sanitize and validate input */ /** @var {Array} - [vErr false || {object}, v {object} || false] */ /** Error: Invalid input */ /** --- Find route match and user type match */ /**
   * Wildcard string match
   *
   * This function always takes the FIRST MATCH it finds.
   *
   * @param {String} str - the route to test
   * @param {String} accessRule - wildcard accessList route
   * @return {Boolean}
   */ /** Choose the server or client list */ /** Loop through all wildcard routes from the accessList */ /**
     * Try to match the route and user type.
     * If accessRule[0] is empty, it will match any user type.
     */ /** accessRule[1] is empty, so this route does not require login. */ /** accessRule[0] matches the user type. */ /** No type match */ /** Log this! */