# graphql-auth-keeper

> Endpoint Auth Keeper for GraphQL Application

Latest version **0.2.13** (published 2018-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-auth-keeper
pnpm add graphql-auth-keeper
yarn add graphql-auth-keeper
bun add graphql-auth-keeper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.13 |
| Published | 2018-11-02 |
| First published | 2018-04-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Chia Yu Pai |
| Maintainers | fantasywind |
| Keywords | GraphQL, Koa, Express, ACL, Auth |

## Links

- npm: https://www.npmjs.com/package/graphql-auth-keeper
- Repository: https://github.com/fantasywind/graphql-auth-keeper
- Homepage: https://github.com/fantasywind/graphql-auth-keeper#readme
- Issues: https://github.com/fantasywind/graphql-auth-keeper/issues
- npm.io page: https://npm.io/package/graphql-auth-keeper

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.1.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.2.1

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 0.2.13 (latest) — 2018-11-02
- 0.2.12 — 2018-11-02
- 0.2.11 — 2018-11-02
- 0.1.10 — 2018-10-14
- 0.1.9 — 2018-06-01
- 0.1.8 — 2018-06-01
- 0.1.7 — 2018-06-01
- 0.1.6 — 2018-05-03
- 0.1.5 — 2018-05-02
- 0.1.4 — 2018-05-02
- 0.1.3 — 2018-04-29
- 0.1.2 — 2018-04-29
- 0.1.1 — 2018-04-29
- 0.1.0 — 2018-04-29

## README

# graphql-auth-keeper
Endpoint Auth Keeper for GraphQL Application

## Apollo Server Support
```javascript
const authKeeper = new GraphQLAuthKeeper({
  secret: 'JWT_SECRET',
});

const server = new ApolloServer(authKeeper.apolloServerOptions({ schema }));
```

## Example

### Member Action Check
```javascript
import {
  GraphQLInt,
  GraphQLNonNull,
  GraphQLString,
} from 'graphql';
import Koa from 'koa';
import Router from 'koa-router';
import koaBody from 'koa-bodyparser';
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa';
import GraphQLAuthKeeper, { authKeeper } from 'graphql-auth-keeper';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { db } from './db';
import { memberType } from './memberType';

const CREATE_MEMBER = {
  name: 'Create Member',
  code: 1,
};

const meQuery = {
  type: memberType,
  resolve: authKeeper({
    onFailed: new Error('Auth Failed'),
    onlineData: true,
  })(member => member),
};

const createMemberMutation = {
  type: GraphQLInt,
  args: {
    account: {
      type: new GraphQLNonNull(GraphQLString),
    },
    password: {
      type: new GraphQLNonNull(GraphQLString),
    },
  },
  resolve: authKeeper({
    onFailed: new Error('Auth Failed'),
    actions: CREATE_MEMBER,    
  })(async (member, {
    account,
    password,
  }) => {
    const createdMember = await db.models.Member.create({
      account,
      password,
      CreatorId: member.id,
    });

    return createdMember.id;
  }),
};

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      me: meQuery,
    },
  }),
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      createMember: createMemberMutation,
    },
  }),
});

const app = new Koa();
const router = new Router();
const authKeeper = new GraphQLAuthKeeper({
  syncFn: payload => db.models.Member.findOne({
    where: {
      id: payload.id,
    },
  }),
  secret: 'JWT_SECRET',
});

router.post('/graphql', koaBody(), graphqlKoa(authKeeper.middleware({
  schema,
})));

app.use(router.routes());
app.use(router.allowedMethods());

const server = createServer(app.callback());

SubscriptionServer.create({
  schema,
  subscribe,
  execute,
  onOperation: authKeeper.subscriptionOperationMiddleware(),
}, {
  server,
  path: '/graphql',
});

server.listen(3000);
```

## Keeper Options

- secret(string)[required]: JWT Secret
- syncFn(Function): Should return live data by payload info

## Route Options

- logined(boolean): Require valid JWT token or not
- actions(Action | Array<Action>): Required actions
- onlineData(boolean): Should return online (syncd) data on resolve payload
- onFailed(Error | Function | any): If Error given will throw it. Function will execute and return it result or return data directly.
- orMode(boolean): The actions should be match all or partial.

## Context added by authKeeper

- jwtPayload(any): JWT payload from token
- authPayload(any): JWT payload from token, if syncFn set, the result will overwrite this value

---
_Source: https://npm.io/package/graphql-auth-keeper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
