npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-auth-keeper

v0.2.13

Published

Endpoint Auth Keeper for GraphQL Application

Downloads

48

Readme

graphql-auth-keeper

Endpoint Auth Keeper for GraphQL Application

Apollo Server Support

const authKeeper = new GraphQLAuthKeeper({
  secret: 'JWT_SECRET',
});

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

Example

Member Action Check

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): 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