Registry / security / graphql-passport

graphql-passport

JSON →
library0.6.8jsnpmunverified

A library providing a Passport.js strategy (GraphQLLocalStrategy) and context builder utilities for integrating authentication into GraphQL servers (Express + Apollo Server). Version 0.6.8 ships TypeScript types and supports Express 4.x, Passport 0.x, subscriptions-transport-ws 0.x, and ws 7.x / 8.x. It simplifies authenticating users via mutations by wrapping passport.authenticate and passport.login into promises accessible from the GraphQL context, and also supports session-based auth and subscription user resolution. Compared to alternatives like graphql-auth, it directly leverages the Passport.js ecosystem.

npm install graphql-passport
INSTALL
IMPORT
SIG · GRAPHQL-PASSPORT
G
graphql-passport
securityjavascriptv0.6.8
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GraphQLLocalStrategy
import { GraphQLLocalStrategy } from 'graphql-passport'
import GraphQLLocalStrategy from 'graphql-passport'
Named export only; default import will fail.
buildContext
import { buildContext } from 'graphql-passport'
const buildContext = require('graphql-passport').buildContext
Correct for both ESM and CJS; named export.
createOnConnect
import { createOnConnect } from 'graphql-passport'
Named export for subscription support; not always needed.

Sets up a basic GraphQL server with passport authentication using graphql-passport. Defines a local strategy, builds context, and implements login mutation.

import express from 'express'; import { ApolloServer } from 'apollo-server-express'; import passport from 'passport'; import { GraphQLLocalStrategy, buildContext } from 'graphql-passport'; const users = [{ email: 'test@test.com', password: 'password', id: 1 }]; passport.use(new GraphQLLocalStrategy((email, password, done) => { const user = users.find(u => u.email === email && u.password === password); done(null, user || false); })); const app = express(); app.use(passport.initialize()); const typeDefs = ` type Query { currentUser: ID } type Mutation { login(email: String!, password: String!): ID } `; const resolvers = { Query: { currentUser: (_, __, context) => context.getUser()?.id, }, Mutation: { login: async (_, { email, password }, context) => { const { user } = await context.authenticate('graphql-local', { email, password }); return user.id; }, }, }; const server = new ApolloServer({ typeDefs, resolvers, context: ({ req, res }) => buildContext({ req, res }), }); server.applyMiddleware({ app }); app.listen(4000, () => console.log('Server running on http://localhost:4000/graphql'));
Debug
Known issues
gotchaauthenticate and login are not available inside subscription resolvers because subscriptions use WebSockets and don't pass Express middleware.
fix
Use createOnConnect to extract user during subscription connection; do not call authenticate/login in subscription resolvers.
affects: >=0.1.0
deprecatedsubscriptions-transport-ws is deprecated; use graphql-ws or newer subscription libraries.
fix
Migrate to graphql-ws and use Apollo Server's built-in subscription support or a compatible plugin.
affects: >=0.6.0
gotchabuildContext must be called with both req and res objects; passing only req will cause errors when calling login/logout.
fix
Always pass the full { req, res } object to buildContext.
affects: >=0.1.0
breakingThe strategy name 'graphql-local' changed from 'local' to 'graphql-local' between versions 0.2.x and 0.3.x.
fix
Use 'graphql-local' as the strategy name; if upgrading from 0.2.x, update calls to context.authenticate('graphql-local', ...).
affects: >=0.3.0
gotchaIf using express-session, you must call context.login(user) after authenticate to persist the session.
fix
Add context.login(user) after successful authentication when sessions are enabled.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: context.authenticate is not a function
buildContext not used or called incorrectly; context lacks passport methods.
fix
Ensure buildContext({ req, res }) is used in Apollo Server's context option.
Error: Unknown authentication strategy "graphql-local"
Strategy not registered with passport; forgot passport.use(new GraphQLLocalStrategy(...)).
fix
Register the strategy with passport.use before creating the Apollo Server.
TypeError: Cannot read properties of undefined (reading 'user') from passport.authenticate
Passport middleware not applied (passport.initialize() missing).
fix
Add app.use(passport.initialize()) and app.use(passport.session()) if using sessions.
Upgrade
Version history
0.6.8latest on npm
Audit
Dependencies
expressrequiredPeer dependency; required for integration with Express-based Apollo Server.
passportrequiredPeer dependency; core authentication library.
subscriptions-transport-wsoptionalPeer dependency; needed for subscription support.
wsoptionalPeer dependency; WebSocket library for subscriptions.
Agent activity
14 hits · last 30 days
node
14
Resources
graphql-passport — npm install graphql-passport · libregistry