Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/utkarsh867/typegraphql-authchecker
Use custom rules in TypeGraphQL Authorized()
https://github.com/utkarsh867/typegraphql-authchecker
authorization graphql
Last synced: about 1 month ago
JSON representation
Use custom rules in TypeGraphQL Authorized()
- Host: GitHub
- URL: https://github.com/utkarsh867/typegraphql-authchecker
- Owner: utkarsh867
- License: mit
- Created: 2021-08-19T18:01:54.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-03T13:26:37.000Z (10 months ago)
- Last Synced: 2024-09-28T21:42:30.843Z (about 2 months ago)
- Topics: authorization, graphql
- Language: TypeScript
- Homepage:
- Size: 344 KB
- Stars: 27
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeGraphQL AuthChecker
Write rules for `Authorized()` decorator of [TypeGraphQL](https://github.com/MichalLytek/type-graphql) resolvers.
## Introduction
I 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).
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`](https://typegraphql.com/docs/authorization.html#how-to-use).
```js
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:
```js
import { Rule } from "typegraphql-authchecker";
import { Context } from "../context";export const isAuthenticated: Rule = ({ context }) => {
if (context.user) {
return true;
}
return false;
};
````Context` in the `Rule` is the type of your GraphQL server context.
Use this rule for a resolver:
```js
@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.
```js
@Authorized([isAuthenticated, isAdmin])
```Another example, to check if user is admin `or` if user is a member, use `OR` conditional:
```js
@Authorized([{OR: [isAdmin, isMember]}])
```The conditionals can be nested, for example:
```js
@Authorized([{ OR: [isAdmin, { AND: [isAuthenticated, isOwner]} ] }])
```## Want to help?
Thank you! Please open issues and pull requests on GitHub!