npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typegraphql-authchecker

v0.1.5

Published

Custom authcheckers for TypeGraphql made simple.

Downloads

767

Readme

TypeGraphQL AuthChecker

Write rules for Authorised() decorator of TypeGraphQL resolvers.

Introduction

I wanted to make rules for using Authorised decorator in TypeGraphQL resolvers just like I used to make them for graphql-shield.

TypeGraphQL allows creating custom authCheckers to use authorization rules. However, it seems limited by the fact that it checks against roles and not rules that can be defined by the developer. Additionally, there seems to be no way to define conditionals such as AND or OR in the authChecker unless implemented by the user from scratch.

This library allows you to define rules for Authorised() decorator in TypeGraphQL resolvers.

Usage

Import the authChecker from typegraphql-authchecker. Then use the authChecker as TypeGraphQL's custom authChecker.

import authChecker from "typegraphql-authchecker";

// Use the authChecker as the custom authChecker for TypeGraphQL
buildSchema({
  resolvers,
  emitSchemaFile: true,
  authChecker: authChecker,
}).then((schema) => {});

Now, you can start creating rules and start using them!

For example, to check if user is authenticated:

import { Rule } from "typegraphql-authchecker";
import { Context } from "../context";

export const isAuthenticated: Rule<Context> = ({ context }) => {
  if (context.user) {
    return true;
  }
  return false;
};

Context in the Rule<Context> is the type of your GraphQL server context.

Use this rule for a resolver:

@Resolver()
export class UserResolver {
  @Authorized(isAuthenticated)
  @Query((returns) => User)
  async me(@Ctx() ctx: Context) {
    return ctx.user;
  }
}

Using AND, OR and NOT conditionals

Rules can be combined using AND, OR and NOT conditionals to perform complex authorization checks.

For example, by default, this will check if user is authenticated and if user is an admin.

@Authorized([isAuthenticated, isAdmin])

Another example, to check if user is admin or if user is a member, use OR conditional:

@Authorized([{OR: [isAdmin, isMember]}])

The conditionals can be nested, for example:

@Authorized([{ OR: [isAdmin, { AND: [isAuthenticated, isOwner]} ] }])

Want to help?

Thank you! Please open issues and pull requests on GitHub!