server/controllers/authController.js
/** Authorize user's login status and access based on user type */
Authorize user's login status and access based on user type
Authorize user ' s login status and access based on user type
async function authorize(req, res, next) {
  const isServerRoute = !req.body.route
  const route = isServerRoute ? req.path : req.body.route
  let input = {}
  input.route = route
  input.accessToken = req.headers.authorization || ''
  input.refreshToken = req.cookies.refreshToken || ''
  const [vErr, v] = validate(
    'route, accessToken, refreshToken',
    input,
    authSchema
  )
  if (vErr || !v) return res.json(joiResponse(vErr))
  const loginData = checkLogin(v.accessToken, v.refreshToken)
  const type = loginData.type || 'none'
  const access = checkAccess(v.route, type, isServerRoute)
  if (access === 'noRouteMatch')
    return res.json({ status: 'error', code: '404' })
  if (access === 'noTypeMatch') {
    if (loginData === false || loginData === {})
      return res.json({ status: 'error', code: 'notLoggedIn' })
    return res.json({ status: 'error', code: '401' })
  }
  if (access === 'allMatch' || access === 'typeMatch') {
    if (isServerRoute) return next()
    if (loginData === false || loginData === {})
      return res.json({ status: 'success', code: 'loggedOut' })
    return res.json({
      status: 'success',
      code: 'loggedIn',
      data: {
        id: loginData.id,
        email: loginData.email,
        name: loginData.name,
        type: loginData.type,
      },
    })
  }
}
async function authorize ( req , res , next ) { const isServerRoute = ! req . body . route const route = isServerRoute ? req . path : req . body . route let input = { } input . route = route input . accessToken = req . headers . authorization || '' input . refreshToken = req . cookies . refreshToken || '' const [ vErr , v ] = validate ( 'route, accessToken, refreshToken' , input , authSchema ) if ( vErr || ! v ) return res . json ( joiResponse ( vErr ) ) const loginData = checkLogin ( v . accessToken , v . refreshToken ) const type = loginData . type || 'none' const access = checkAccess ( v . route , type , isServerRoute ) if ( access === 'noRouteMatch' ) return res . json ( { status : 'error' , code : '404' } ) if ( access === 'noTypeMatch' ) { if ( loginData === false || loginData === { } ) return res . json ( { status : 'error' , code : 'notLoggedIn' } ) return res . json ( { status : 'error' , code : '401' } ) } if ( access === 'allMatch' || access === 'typeMatch' ) { if ( isServerRoute ) return next ( ) if ( loginData === false || loginData === { } ) return res . json ( { status : 'success' , code : 'loggedOut' } ) return res . json ( { status : 'success' , code : 'loggedIn' , data : { id : loginData . id , email : loginData . email , name : loginData . name , type : loginData . type , } , } ) } }
Authorize user's login status and access based on user type
Authorize user ' s login status and access based on user type
/** --- Check if server-route or client-route */ /**
   * If no client route was sent (req.body.route) then
   * this is an $axios call to the server route (req.path)
   */ /** --- Validate input */ /** Prepare input for validation */ /** Sanitize and validate route and tokens */ /** @var {Array} - [vErr false || {object}, v {object} || false] */ /** Error: Invalid input */ /** --- Check if user is logged in */ /** @var {Boolean|Object} isLoggedIn - false or decoded user data */ /** @var {String} type - The user type from the decoded token data */ /** --- Check if user type has access */ /** @var {String} access - The returned access code */ /** Error: ROUTE is not in the access list (client or server) */ /** Error: USER TYPE not found in the access list (client or server) */ /** User is not logged in */ /** User is logged in */ /** --- Success */ /** Server route -- Continue to controller. */ /** Client route -- User is not logged in */ /** Client route -- User is logged in. Return user data */