Big news!Introducing Socket AI - ChatGPT-Powered Threat Analysis. Learn more
Socket
Log inDemoInstall

@theydo/graphql-directive-requires-authentication

Package Overview
Dependencies
3
Maintainers
3
Versions
19
Issues
File Explorer

Advanced tools

@theydo/graphql-directive-requires-authentication

GraphQL directive for checking authentication through context

    0.1.13latest
    GitHub

Version published
Maintainers
3
Weekly downloads
197
increased by15.88%

Weekly downloads

Readme

Source

GraphQL Directive: @requiresAuthentication

The point of this directive is not to validate JWT tokens, since this is frequently a project-specific implementation. Instead it is expected that resolving token status is done in the Apollo getContext hook, where the validated token is used to extend the context object (user information).

This directive will only assert the "isAuthenticated" status coming from the context object.

Usage

To use the directive, you should start by adding the directive to your GraphQL schema:

@directive requiresAuthentication on FIELD_DEFINITION

After this, you can create the directive by passing an isAuthenticated resolver (and optionally a custom error) and the directive will do the rest:

const requiresAuthentication = requiresAuthenticationDirective<Context>({ isAuthenticated: (context) => !!context.auth?.token, throwError: new MyCustomError('WITH_CUSTOM_ERROR_CODE'), }) new ApolloServer({ schemaDirectives: { requiresAuthentication, }, })

Keywords

FAQs

Last updated on 04 May 2021

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket
support@socket.devSocket SOC 2 Logo

Product

  • Package Issues
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc