How to use the apollo-server-koa.AuthenticationError function in apollo-server-koa

To help you get started, we’ve selected a few apollo-server-koa 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 velopert / velog-server / src / graphql / post.ts View on Github external
unlikePost: async (parent: any, args, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }

      // find post
      const postRepo = getRepository(Post);
      const post = await postRepo.findOne(args.id);

      if (!post) {
        throw new ApolloError('Post not found', 'NOT_FOUND');
      }

      // check already liked
      const postLikeRepo = getRepository(PostLike);
      const postLike = await postLikeRepo.findOne({
        where: {
          fk_post_id: args.id,
          fk_user_id: ctx.user_id
github fullstack-build / fullstack-one / packages / graphql / lib / koaMiddleware.ts View on Github external
}
    if (_.get(error, "name", null) === "AuthenticationError") {
      return handleGenericError(AuthenticationError, "AuthenticationError: Details hidden.");
    }
    if (_.get(error, "name", null) === "ForbiddenError") {
      return handleGenericError(ForbiddenError, "ForbiddenError: Details hidden.");
    }

    // Try to map other errors to Apollo predefined errors. Useful when writing pg-functions which cannot return a specific Error Object
    if (error.message.indexOf("AUTH.THROW.USER_INPUT_ERROR") >= 0) {
      logger.trace(error);
      return new UserInputError("Bad user input.");
    }
    if (error.message.indexOf("AUTH.THROW.AUTHENTICATION_ERROR") >= 0) {
      logger.trace(error);
      return new AuthenticationError("Authentication required.");
    }
    if (error.message.indexOf("AUTH.THROW.FORBIDDEN_ERROR") >= 0) {
      logger.trace(error);
      return new ForbiddenError("Access forbidden.");
    }

    if (_.get(error, "name", null) === "ApolloError") {
      return handleGenericError(ApolloError, "ApolloError: Details hidden.");
    }
    if (_.get(error, "name", null) === "GraphQLError") {
      return handleGenericError(GraphQLError, "GraphQLError: Details hidden.");
    }
    // Log all internal errors as error here => Everything else is just trace
    logger.error(error);

    // For all other errors just return a Internal server error
github velopert / velog-server / src / graphql / tag.ts View on Github external
mergeTag: async (parent: any, { selected, merge_to }: MergeTagParams, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }
      const isAdmin = await AdminUser.checkAdmin(ctx.user_id);
      if (!isAdmin) {
        throw new ApolloError('You are not admin', 'NO_PERMISSION');
      }
      const tagRepo = getRepository(Tag);

      // 0. check tag validity
      const [selectedTag, mergeToTag] = await Promise.all(
        [selected, merge_to].map(tag => tagRepo.findOne(tag))
      );

      if (!selectedTag) {
        throw new ApolloError(`Tag ${selected} is not found`, 'NOT_FOUND');
      }
github velopert / velog-server / src / graphql / series.ts View on Github external
async function getSeriesIfValid(seriesId: string, userId: string | null) {
  if (!userId) {
    throw new AuthenticationError('Not Logged In');
  }
  const seriesRepo = getRepository(Series);
  const series = await seriesRepo.findOne(seriesId);
  if (!series) {
    throw new ApolloError('Series not found', 'NOT_FOUND');
  }
  if (series.fk_user_id !== userId) {
    throw new ApolloError('This series is not yours', 'NO_PERMISSION');
  }

  return series;
}
github velopert / velog-server / src / graphql / user.ts View on Github external
email: (parent: User, _: any, context: any) => {
      if (context.user_id !== parent.id) {
        throw new AuthenticationError('No permission to read email address');
      }
      return parent.email;
    },
    series_list: async (parent: User, _: any, { loaders }) => {
github velopert / velog-server / src / graphql / comment.ts View on Github external
writeComment: async (parent: any, args, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }
      const { post_id, comment_id, text } = args as WriteCommentArgs;
      const post = await getRepository(Post).findOne(post_id);
      if (!post) {
        throw new ApolloError('Post not found', 'NOT_FOUND');
      }
      const commentRepo = getRepository(Comment);
      const comment = new Comment();

      if (comment_id) {
        const commentTarget = await commentRepo.findOne(comment_id);
        if (!commentTarget) {
          throw new ApolloError('Target comment is not found', 'NOT_FOUND');
        }
        comment.level = commentTarget.level + 1;
        comment.reply_to = comment_id;
github velopert / velog-server / src / graphql / user.ts View on Github external
update_about: async (parent: any, args: any, ctx) => {
      if (!ctx.user_id) {
        throw new AuthenticationError('Not Logged In');
      }
      const userProfileRepo = getRepository(UserProfile);
      const profile = await userProfileRepo.findOne({
        where: {
          fk_user_id: ctx.user_id
        }
      });
      const { about } = args as { about: string };
      if (!profile) {
        throw new ApolloError('Failed to retrieve user profile');
      }
      profile.about = about || '';
      await userProfileRepo.save(profile);
      return profile;
    }
  }