graphql - GraphQL 中的社交身份验证

标签 graphql passport.js express-graphql

我正在创建一个依赖于 Express 和 GraphQL 的后端,它将为客户端应用程序(Android 和 React)提供服务。

我一直在关注这个article关于如何使用 GraphQL 中的社交身份验证 passport.js .

文章使用passport-google-token策略,基于 Apollo-server 但我个人更喜欢使用 express-graphql .

将我的 Android 应用程序设置为使用 google auth 并尝试将突变发送到服务器后,我收到此错误

Google info:  { InternalOAuthError: failed to fetch user profile
    at E:\_Projects\myProject\myProject-backend\node_modules\passport-google-token\lib\passport-google-token\strategy.js:114:28
    at passBackControl (E:\_Projects\myProject\myProject-backend\node_modules\oauth\lib\oauth2.js:132:9)
    at IncomingMessage.<anonymous> (E:\_Projects\myProject\myProject-backend\node_modules\oauth\lib\oauth2.js:157:7)
    at IncomingMessage.emit (events.js:194:15)
    at endReadableNT (_stream_readable.js:1125:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    name: 'InternalOAuthError',
        message: 'failed to fetch user profile',
        oauthError:
    { statusCode: 401,
        data:
        '{\n  "error": {\n    "code": 401,\n    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",\n    "status": "UNAUTHENTICATED"\n  }\n}\n' } }

我相信我传递的 token 可能没有到达它应该到达的地方,但我不知道如何解决。

我在这里测试了我的 token https://oauth2.googleapis.com/tokeninfo?id_token=MyToken并且它们工作正常。

这是 app.js 中的 graphQL 配置

app.use('/graphql', graphqlHTTP((req, res) => ({
    schema,
    graphiql: true,
    context: {req, res}
})));

这是谷歌身份验证突变

        googleAuth: {
            type: authPayLoad,
            args: {token: {type: new GraphQLNonNull(GraphQLString)}},
            async resolve(_, {token}, {req, res}) {
                req.body = {
                    ...req.body,
                    access_token: token,
                };

                try {
                    const {data, info} = await authenticateGoogle(req, res);
                    console.log("Google data: ", data);

                    if (data) {
                        const user = await User.upsertGoogleUser(data);

                        if (user) {
                            return ({
                                name: user.name,
                                username: user.username,
                                token: user.generateJWT(),
                            });
                        }
                    }

                    if (info) {
                        console.log("Google info: ", info);

                        switch (info.code) {
                            case 'ETIMEDOUT':
                                return (new Error('Failed to reach Google: Try Again'));
                            default:
                                return (new Error('something went wrong'));
                        }
                    }
                    return (Error('server error'));

                } catch (e) {
                    console.log("Error: ", e);
                    return e
                }
            },
        },

这是我的身份验证 Controller

const passport = require('passport');
const {Strategy: GoogleTokenStrategy} = require('passport-google-token');

// GOOGLE STRATEGY
const GoogleTokenStrategyCallback = (accessToken, refreshToken, profile, done) => done(null, {
    accessToken,
    refreshToken,
    profile,
});

passport.use(new GoogleTokenStrategy({
    clientID: 'MY_CLIET_ID',
    clientSecret: 'SERVER-SECRET'
}, GoogleTokenStrategyCallback));

module.exports.authenticateGoogle = (req, res) => new Promise((resolve, reject) => {
    passport.authenticate('google-token', {session: false}, (err, data, info) => {
        if (err) reject(err);
        resolve({data, info});
    })(req, res);
});

我预计当客户端应用程序提交带有 token 作为arg的突变时,请求将被发送到谷歌并返回用户数据。我该如何解决这个问题。

最佳答案

passport-google-token已存档,对我来说似乎已弃用。你为什么不试试passport-token-google 。 可以类似的方式使用。

passport.use(new GoogleStrategy({
        clientID: keys.google.oauthClientID,
        clientSecret: keys.google.oauthClientSecret
    },
    function (accessToken, refreshToken, profile, done) {
        return done(null, {
            accessToken,
            refreshToken,
            profile,
        });
    }
));

module.exports.authenticate = (req, res) => new Promise((resolve, reject) => {
    passport.authenticate('google-token', {session: false}, (err, data, info) => {
        if (err) reject(err);
        resolve({data, info});
    })(req, res);
});

希望这有帮助。

关于graphql - GraphQL 中的社交身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55952357/

相关文章:

npm - 可以使用 pnpm 仅使用一个软件包版本吗?

javascript - 存在 Express session 问题的 Node

node.js - 安装 graphql-tools 和 graphql-express 后。控制台中出现错误消息 'Cannot find name ' AsyncIterator'

graphql - 是否可以使用 GraphQLList 从多个表中获取数据

node.js - Passport JWT - 未经授权

node.js - 在上下文(express.js 中的请求)对象中附加属性

reactjs - React Apollo GraphQL 搜索/过滤

Python GraphQL 如何声明自引用 Graphite 烯对象类型

javascript - GraphQL 和 Sequelize 不返回插入的对象

node.js - 如何在没有客户端的情况下使用 Facebook 的 Passport 在 Node.js 中构建 REST 服务?