@wasc/graphql-authdirective
# [![authDirective](docs/carbon.png)](https://github.com/wasc-io/graphql-authDirective)
Last updated 4 days ago by nicolaischmid .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @wasc/graphql-authdirective 
SYNC missed versions from official npm registry.

authDirective

authDirective

Overview

Graphql-authDirective is a GraphQL-Directive for authenticating specific fields and types on your schema and also checking if the user has a valid scope.

Preperation

The directive expects a auth-property on the graphql-context (in most cases the express-request). It should contain a isAuthenticated Boolean and an array of strings with OAuth scopes on the scope property of the auth object.

{
    auth: {
        isAuthenticated: true, // or false
        scope: ['user:properties:language:*', 'payments:*'],
    }
}

If you want to create a middleware which authenticates your user before the graphql schema, make sure to pass the request as a graphql-context to the resolvers. Look at the ApolloServer configuration below to verify your configuration.

Usage

Currently this package is only tested with ApolloServer, and will only work with ApolloServer, since it uses apollo-specific errors to alert when the user is not authenticated.

To use it with ApolloServer 2.0 install it, import it and set it as a schema directive in the Server-Configuration:

npm i @wasc/graphql-authDirective
import authenticateDirective from "@wasc/graphql-authDirective";

const apolloServer = new ApolloServer({
  typeDefs,
  resolvers,
  schemaDirectives: {
    authenticated: authenticateDirective
  },
  /**
   * This one is important, if you are using a middleware to check the users token
   * and set the auth object (described above), make sure to include the request
   * as the graphql-context
   */
  context: ({ req }) => req
});

and declare it in your schema:

directive @authenticated(scope: [String]) on FIELD_DEFINITION | OBJECT

you can now specify the authentication of specific fields and Types

type Query {
  language: String! @authenticated(scope: ["user:properties:language:read"])
}

Have fun coding!

Current Tags

  • 1.0.5                                ...           latest (4 days ago)

10 Versions

  • 1.0.5                                ...           4 days ago
  • 1.0.4                                ...           4 days ago
  • 1.0.3                                ...           25 days ago
  • 1.0.2                                ...           3 months ago
  • 1.0.1                                ...           7 months ago
  • 1.0.0                                ...           a year ago
  • 0.3.0                                ...           a year ago
  • 0.2.3                                ...           a year ago
  • 0.2.2                                ...           a year ago
  • 0.2.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 54
Last Day 0
Last Week 54
Last Month 20
Dependencies (3)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |