{"id":14976227,"url":"https://github.com/utkarsh867/typegraphql-authchecker","last_synced_at":"2025-04-03T02:11:45.235Z","repository":{"id":45109336,"uuid":"398032204","full_name":"utkarsh867/typegraphql-authchecker","owner":"utkarsh867","description":"Use custom rules in TypeGraphQL Authorized()","archived":false,"fork":false,"pushed_at":"2024-10-22T07:33:39.000Z","size":183,"stargazers_count":27,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T08:41:40.134Z","etag":null,"topics":["authorization","graphql"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/utkarsh867.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-19T18:01:54.000Z","updated_at":"2024-09-28T14:29:08.000Z","dependencies_parsed_at":"2024-02-03T14:30:25.707Z","dependency_job_id":"3161f5e3-a969-4893-94a7-ae2d6493869c","html_url":"https://github.com/utkarsh867/typegraphql-authchecker","commit_stats":{"total_commits":25,"total_committers":7,"mean_commits":"3.5714285714285716","dds":0.52,"last_synced_commit":"efb0b539864bc3cf4481237b7f51093dffc42882"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utkarsh867%2Ftypegraphql-authchecker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utkarsh867%2Ftypegraphql-authchecker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utkarsh867%2Ftypegraphql-authchecker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utkarsh867%2Ftypegraphql-authchecker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/utkarsh867","download_url":"https://codeload.github.com/utkarsh867/typegraphql-authchecker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246922248,"owners_count":20855345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authorization","graphql"],"created_at":"2024-09-24T13:53:32.356Z","updated_at":"2025-04-03T02:11:45.209Z","avatar_url":"https://github.com/utkarsh867.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TypeGraphQL AuthChecker\n\nWrite rules for `Authorized()` decorator of [TypeGraphQL](https://github.com/MichalLytek/type-graphql) resolvers.\n\n## Introduction\n\nI wanted to make rules for using Authorized decorator in TypeGraphQL resolvers just like I used to make them for [graphql-shield](https://github.com/maticzav/graphql-shield).\n\nTypeGraphQL 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.\n\nThis library allows you to define rules for `Authorised()` decorator in TypeGraphQL resolvers.\n\n## Usage\n\nImport the `authChecker` from `typegraphql-authchecker`. Then use the `authChecker` as [TypeGraphQL's custom `authChecker`](https://typegraphql.com/docs/authorization.html#how-to-use).\n\n```js\nimport authChecker from \"typegraphql-authchecker\";\n\n// Use the authChecker as the custom authChecker for TypeGraphQL\nbuildSchema({\n  resolvers,\n  emitSchemaFile: true,\n  authChecker: authChecker,\n}).then((schema) =\u003e {});\n```\n\nNow, you can start creating rules and start using them!\n\nFor example, to check if user is authenticated:\n\n```js\nimport { Rule } from \"typegraphql-authchecker\";\nimport { Context } from \"../context\";\n\nexport const isAuthenticated: Rule\u003cContext\u003e = ({ context }) =\u003e {\n  if (context.user) {\n    return true;\n  }\n  return false;\n};\n```\n\n`Context` in the `Rule\u003cContext\u003e` is the type of your GraphQL server context.\n\nUse this rule for a resolver:\n\n```js\n@Resolver()\nexport class UserResolver {\n  @Authorized(isAuthenticated)\n  @Query((returns) =\u003e User)\n  async me(@Ctx() ctx: Context) {\n    return ctx.user;\n  }\n}\n```\n\n### Using `AND`, `OR` and `NOT` conditionals\n\nRules can be combined using `AND`, `OR` and `NOT` conditionals to perform complex authorization checks.\n\nFor example, by default, this will check if user is authenticated `and` if user is an admin.\n\n```js\n@Authorized([isAuthenticated, isAdmin])\n```\n\nAnother example, to check if user is admin `or` if user is a member, use `OR` conditional:\n\n```js\n@Authorized([{OR: [isAdmin, isMember]}])\n```\n\nThe conditionals can be nested, for example:\n\n```js\n@Authorized([{ OR: [isAdmin, { AND: [isAuthenticated, isOwner]} ] }])\n```\n\n## Want to help?\n\nThank you! Please open issues and pull requests on GitHub!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futkarsh867%2Ftypegraphql-authchecker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Futkarsh867%2Ftypegraphql-authchecker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futkarsh867%2Ftypegraphql-authchecker/lists"}