Authentication 使用Passport认证graphcool瑜伽
我正在做一个项目,使用graphcool瑜伽和prisma绑定。希望使用passport的本地、承载、Github和Twitter策略设置身份验证。下面是我的graphql查询的样子Authentication 使用Passport认证graphcool瑜伽,authentication,passport.js,graphql-js,graphcool,prisma,Authentication,Passport.js,Graphql Js,Graphcool,Prisma,我正在做一个项目,使用graphcool瑜伽和prisma绑定。希望使用passport的本地、承载、Github和Twitter策略设置身份验证。下面是我的graphql查询的样子 user: (root, args, context, info) => { const { id } = args; if(!passport.authenticate('bearer')(context)){ throw new Error('Not Authorised'); } if (!id
user: (root, args, context, info) => {
const { id } = args;
if(!passport.authenticate('bearer')(context)){
throw new Error('Not Authorised');
}
if (!id) {
throw new Error('Id cannot be empty');
}
return context.db.query.user(
{
where: {
id: id,
active: true,
},
},
info,
);}
My auth.js,其中我实施了持证人代币的passport策略:
import express from 'express';
import passport from 'passport';
import { Strategy as BearerStrategy } from 'passport-http-bearer';
import jwt from 'jsonwebtoken';
passport.use(new BearerStrategy((token, done) => {
jwt.verify(token, process.env.JWT_SECRET, function(err, decoded) {
if (err) return done(null, err);
if (decoded.sub) {
done(null, decoded.sub || undefined);
}
});
}));
const middleware = express();
middleware.use(passport.initialize());
middleware.use(passport.session());
module.exports = {
authMiddleware: middleware
};
最后,我将其用作中间件:
const server = new GraphQLServer({
typeDefs: 'app/schema.graphql', // application api schema
resolvers,
context: req => ({
...req,
db
}),
});
server.express.use(authMiddleware);
这是我在阅读了各种资料后可以写的,但不起作用。有没有正确的最佳方法?您收到的错误消息是什么?没有错误消息。只是我无法将身份验证中间件添加到特定的路由。好的,你能解决这个问题吗?如果没有,请修复这篇文章,包括所有相关的代码,我很乐意帮助,因为我现在正在从事类似的工作。