https://github.com/fantasywind/graphql-auth-keeper
Endpoint Auth Keeper for GraphQL Application
https://github.com/fantasywind/graphql-auth-keeper
Last synced: about 1 year ago
JSON representation
Endpoint Auth Keeper for GraphQL Application
- Host: GitHub
- URL: https://github.com/fantasywind/graphql-auth-keeper
- Owner: fantasywind
- License: mit
- Created: 2018-04-29T07:57:19.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-02T06:52:57.000Z (over 7 years ago)
- Last Synced: 2025-04-14T01:53:44.555Z (about 1 year ago)
- Language: JavaScript
- Size: 15.6 KB
- Stars: 3
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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): 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