How to use the server/helpers/error.APIError function in server

To help you get started, we’ve selected a few server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
if (!user._active) {
    logger.info('user access denied: inactive', {
      email,
      headers: req.headers
    })

    throw new APIError('Account not active', httpStatus.UNAUTHORIZED)
  }

  if (!user.verified && env.NODE_ENV !== env.Environments.Test) { // Skip for tests since verifying requires opening an email
    logger.info('user access denied: not verified', {
      email,
      headers: req.headers
    })

    throw new APIError('Access denied', httpStatus.PRECONDITION_FAILED)
  }

  const token = _generateToken(user)

  res.json({
    token
  })

  logger.info('user logged in', {
    email,
    headers: req.headers
  })
}
github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
if (!user || !user.password || !await user.comparePassword(password)) {
    logger.info('user access denied: invalid user or password', {
      email,
      headers: req.headers
    })

    throw new APIError('Access denied', httpStatus.FORBIDDEN)
  }

  if (!user._active) {
    logger.info('user access denied: inactive', {
      email,
      headers: req.headers
    })

    throw new APIError('Account not active', httpStatus.UNAUTHORIZED)
  }

  if (!user.verified && env.NODE_ENV !== env.Environments.Test) { // Skip for tests since verifying requires opening an email
    logger.info('user access denied: not verified', {
      email,
      headers: req.headers
    })

    throw new APIError('Access denied', httpStatus.PRECONDITION_FAILED)
  }

  const token = _generateToken(user)

  res.json({
    token
  })
github Christilut / node-modern-boilerplate / server / helpers / auth.ts View on Github external
export async function createUser(args: ICreateUserArgs): Promise> {
  validate(args, createUserValidation)

  const existingUser: User = await UserModel.findByEmail(args.email)

  // If user is not unique, return error
  if (existingUser) {
    throw new APIError('Email address is already in use', httpStatus.CONFLICT)
  }

  // If email is unique, create account
  let user = new UserModel({
    email: args.email.toLowerCase()
  })

  user = await user.save()

  // User has no password yet and must set one in the verification link, sent in the pre-save

  return user
}
github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
export async function resetPassword(req, res, next) {
  const token: string = req.body.token
  const password: string = req.body.password

  try {
    const verifiedToken: IForgotPasswordTokenContents = await JWT.verify(token, env.EMAIL_FORGOT_SECRET) as IForgotPasswordTokenContents

    const user = await UserModel.findById(verifiedToken.id)

    if (!user) {
      logger.warn('reset password triggered for non-existant user but token was valid', {
        verifiedJwt: verifiedToken,
        headers: req.headers
      })

      return next(new APIError('User does not exist', httpStatus.UNAUTHORIZED))
    }

    user.password = password

    await user.save()

    const title = 'Password changed'
    const message = ``

    await user.sendMail(
      title,
      message,
      EMAIL_TEMPLATES.Info,
      {
        message
      }
github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
export async function resendVerification(req, res, next) {
  const email: string = req.body.email

  if (!email) {
    return next(new APIError('Missing email parameter', httpStatus.UNAUTHORIZED))
  }

  const user = await UserModel.findByEmail(email)

  if (!user) {
    logger.warn('user resend verification mail triggered for non-existant user', {
      email,
      headers: req.headers
    })

    return next(new APIError('User does not exist', httpStatus.UNAUTHORIZED))
  }

  if (user.verified) {
    logger.warn('user resend verification mail triggered for already verified user', {
      email,
github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
export async function verifyAccount(req, res, next) {
  const { token, password } = req.body

  try {
    const verifiedToken: IVerificationMailTokenContents = await JWT.verify(token, env.EMAIL_VERIFY_SECRET) as IVerificationMailTokenContents

    const user = await UserModel.findById(verifiedToken.id)

    if (!user) {
      logger.warn('account verification triggered for non-existant user but token was valid', {
        userId: verifiedToken.id,
        headers: req.headers
      })

      return next(new APIError('User does not exist', httpStatus.UNAUTHORIZED))
    }

    if (user.verified) {
      logger.warn('user account verification triggered for already verified user', {
        userId: verifiedToken.id,
        headers: req.headers
      })

      return next(new APIError('User already verified', httpStatus.UNAUTHORIZED))
    }

    user.verified = true
    user.password = password

    await user.save()
github Christilut / node-modern-boilerplate / server / helpers / s3.ts View on Github external
export async function downloadFileAsBase64(key: string, bucket: string): Promise {
  const s3Params: IDownloadFileArgs = {
    bucket,
    key
  }

  const file = await downloadFile(s3Params)

  if (!file) {
    message('download as base64: key not found on s3', {
      extra: {
        s3Params
      }
    })

    throw new APIError('lease not found on s3', httpStatus.INTERNAL_SERVER_ERROR)
  }

  return 'data:text/plain;base64,' + file.toString('base64')
}
github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
export async function checkAuthentication(req, res, next) {
  const token: string = req.headers.authorization

  try {
    const verifiedToken = await JWT.verify(token.replace('Bearer ', ''), env.JWT_SECRET) as IJsonWebTokenContents

    const user = await UserModel.findById(verifiedToken.id) as User

    if (!user._active) return next(new APIError('user inactive', httpStatus.FORBIDDEN))

    req.user = user

    next()
  } catch (error) {
    return next(new APIError('invalid token', httpStatus.UNAUTHORIZED))
  }
}
github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
if (!user) {
    logger.warn('user resend verification mail triggered for non-existant user', {
      email,
      headers: req.headers
    })

    return next(new APIError('User does not exist', httpStatus.UNAUTHORIZED))
  }

  if (user.verified) {
    logger.warn('user resend verification mail triggered for already verified user', {
      email,
      headers: req.headers
    })

    return next(new APIError('User already verified', httpStatus.UNAUTHORIZED))
  }

  logger.info('re-sent account verification mail', {
    email,
    headers: req.headers
  })

  await sendVerificationMail(user)

  res.sendStatus(httpStatus.OK)
}
github Christilut / node-modern-boilerplate / server / controllers / auth.controller.ts View on Github external
export async function resendVerification(req, res, next) {
  const email: string = req.body.email

  if (!email) {
    return next(new APIError('Missing email parameter', httpStatus.UNAUTHORIZED))
  }

  const user = await UserModel.findByEmail(email)

  if (!user) {
    logger.warn('user resend verification mail triggered for non-existant user', {
      email,
      headers: req.headers
    })

    return next(new APIError('User does not exist', httpStatus.UNAUTHORIZED))
  }

  if (user.verified) {
    logger.warn('user resend verification mail triggered for already verified user', {
      email,
      headers: req.headers
    })

    return next(new APIError('User already verified', httpStatus.UNAUTHORIZED))
  }

  logger.info('re-sent account verification mail', {
    email,
    headers: req.headers
  })