type-graphql#AuthChecker TypeScript Examples

The following examples show how to use type-graphql#AuthChecker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: auth-checker.ts    From convoychat with GNU General Public License v3.0 6 votes vote down vote up
useAuth: AuthChecker<Context> = (
  { root, args, context, info },
  _roles
) => {
  // here we can read the user from context
  // and check his permission in the db against the `roles` argument
  // that comes from the `@Authorized` decorator, eg. ["ADMIN", "MODERATOR"]
  if (!context.getUser()) {
    throw new AuthenticationError("User not authenticated");
  }
  return true;
}
Example #2
Source File: GraphQLSchema.ts    From liferay-grow with MIT License 6 votes vote down vote up
private customAuthChecker: AuthChecker<MyContext> = ({ context }) => {
    const { AUTH_MIDDLEWARE_ENABLED } = process.env;

    return Boolean(
      !AUTH_MIDDLEWARE_ENABLED ||
        (AUTH_MIDDLEWARE_ENABLED && context.isAuthenticated),
    );

    // here we can read the user from context
    // and check his permission in the db against the `roles` argument
    // that comes from the `@Authorized` decorator, eg. ["ADMIN", "MODERATOR"]
  };
Example #3
Source File: authorizations.ts    From backend with MIT License 5 votes vote down vote up
authChecker: AuthChecker<GraphQLContext> = async ({ context, info, root }, requiredRoles) => {
    assert(requiredRoles.length, "Roles must be passed to AUTHORIZED");
    assert(requiredRoles.every(role => role in Role), "Roles must be of enum Role");
    assert(context.user?.roles, "Roles must have been initialized in context");

    return await accessCheck(context, requiredRoles as Role[], info.parentType?.name as ResolverModelNames, root);
}