How to use the http-status.UNAUTHORIZED function in http-status

To help you get started, we’ve selected a few http-status 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 box / box-node-sdk / lib / box-client.js View on Github external
function isUnauthorizedDueToExpiredAccessToken(response) {
	// There are three cases to consider:
	// 1) The response body is a Buffer. This indicates that the request was malformed (i.e. malformed url) so return false.
	// 2) The status code is UNAUTHORIZED and the response body is an empty object or null. This indicates that the access tokens are expired, so return true.
	// 3) The status code is UNAUTHORIZED and the response body is a non-empty object. This indicates that the 401 was returned for some reason other
	//    than expired tokens, so return false.

	if (Buffer.isBuffer(response.body)) {
		return false;
	}

	var isResponseStatusCodeUnauthorized = response.statusCode === httpStatusCodes.UNAUTHORIZED,
		isResponseBodyEmpty = !response.body || Object.getOwnPropertyNames(response.body).length === 0;
	return isResponseStatusCodeUnauthorized && isResponseBodyEmpty;
}
github waldemarnt / testable-nodejs-api / routes / auth.js View on Github external
      .catch(() => res.sendStatus(HttpStatus.UNAUTHORIZED));
    } else {
github bookbrainz / bookbrainz-site / src / common / helpers / error.js View on Github external
static get status() {
		return status.UNAUTHORIZED;
	}
}
github SoftwareEngineeringDaily / software-engineering-daily-api / server / controllers / job.controller.js View on Github external
update: async (req, res, next) => {
    try {
      const job = await Job
        .findById(req.params.jobId);

      if (!job) {
        return next(new APIError('Job not found', httpStatus.NOT_FOUND));
      }

      if (job.postedUser.toString() !== req.user._id.toString()) {
        return next(new APIError('Not allowed to update a job you did not post', httpStatus.UNAUTHORIZED));
      }

      const today = new Date().getDate();

      if (job.isDeleted || (job.expirationDate && job.expirationDate < today)) {
        return next(new APIError('Not allowed to update this job as it has been deleted', httpStatus.FORBIDDEN));
      }

      const updated = Object.assign(job, req.body);
      await updated.save();

      return res.status(httpStatus.OK).json(transform(updated, true));
    } catch (err) {
      return next(err);
    }
  },
github ridhamtarpara / express-es8-rest-boilerplate / src / api / services / user / user.model.js View on Github external
async findAndGenerateToken(options) {
    const { email, password, refreshObject } = options;
    if (!email) throw new APIError({ message: 'An email is required to generate a token' });

    const user = await this.findOne({ email }).exec();
    const err = {
      status: httpStatus.UNAUTHORIZED,
      isPublic: true,
    };
    if (password) {
      if (user && await user.passwordMatches(password)) {
        return { user, accessToken: user.token() };
      }
      err.message = 'Incorrect email or password';
    } else if (refreshObject && refreshObject.userEmail === email) {
      return { user, accessToken: user.token() };
    } else {
      err.message = 'Incorrect email or refreshToken';
    }
    throw new APIError(err);
  },
github bookbrainz / bookbrainz-site / src / server / helpers / error.js View on Github external
static get status() {
		return status.UNAUTHORIZED;
	}
}
github Kamahl19 / react-starter / backend / src / common / utils / apiErrors.js View on Github external
function UnauthorizedError(error) {
  Error.call(this, error.message);
  Error.captureStackTrace(this, this.constructor);
  this.name = 'UnauthorizedError';
  this.message = error.message;
  this.status = httpStatus.UNAUTHORIZED;
  this.inner = error;
}
github apache / dubbo-admin / dubbo-admin-ui / src / components / http-common.js View on Github external
}, (error) => {
  if (error.message.indexOf('Network Error') >= 0) {
    Vue.prototype.$notify.error('Network error, please check your network settings!')
  } else if (error.response.status === HttpStatus.UNAUTHORIZED) {
    localStorage.removeItem('token')
    localStorage.removeItem('username')
    Vue.prototype.$notify.error('Authorized failed,please login.')
  } else if (error.response.status >= HttpStatus.BAD_REQUEST) {
    Vue.prototype.$notify.error(error.response.data.message)
  }
})
github hagopj13 / node-express-mongoose-boilerplate / src / services / auth.service.js View on Github external
const refreshAuthTokens = async refreshToken => {
  try {
    const refreshTokenDoc = await tokenService.verifyToken(refreshToken, 'refresh');
    const userId = refreshTokenDoc.user;
    await userService.getUserById(userId);
    await refreshTokenDoc.remove();
    return await generateAuthTokens(userId);
  } catch (error) {
    throw new AppError(httpStatus.UNAUTHORIZED, 'Please authenticate');
  }
};
github raphaellima8 / typescript-api / server / api / responses / authSuccess.js View on Github external
function authFail(req, res) {
    res.sendStatus(HTTPStatus.UNAUTHORIZED);
}
exports.authFail = authFail;