将具有Firebase功能的用户同步到Hasura GraphQL

问题描述 投票:0回答:1

我想使用firebase来验证用户身份,然后使用firebase函数将用户插入Hasura,但是firebase函数存在问题。

[当我尝试从应用程序中创建“ registerUser”功能时,可以在下面找到它,它以错误结尾:

Error detected in registerUser:
{"@type":"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.Insight",
"errorGroup":"CLic1cmw6emOsAE",
"errorEvent":{"message":"Error: The uid must be a non-empty string with at most 128 characters.
at FirebaseAuthError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseAuthError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28)\
at new FirebaseAuthError (/srv/node_modules/firebase-admin/lib/utils/error.js:147:16)
at AuthRequestHandler.AbstractAuthRequestHandler.setCustomUserClaims (/srv/node_modules/firebase-admin/lib/auth/auth-api-request.js:996:35)
at Auth.BaseAuth.setCustomUserClaims (/srv/node_modules/firebase-admin/lib/auth/auth.js:342:40)
at exports.registerUser.functions.https.onCall (/srv/index.js:32:18)
at func (/srv/node_modules/firebase-functions/lib/providers/https.js:272:32)
at corsHandler (/srv/node_modules/firebase-functions/lib/providers/https.js:292:44)\n    at cors (/srv/node_modules/cors/lib/index.js:188:7)
at /srv/node_modules/cors/lib/index.js:224:17","eventTime":"2020-06-10T08:25:03.017Z","serviceContext":{"service":"registerUser","resourceType":"cloud_function"}}}

[如果我改为直接通过Firebase控制台创建用户,则我的“ processSignUp”运行但以另一个错误结束:

ReferenceError: fetch is not defined
    at GraphQLClient.<anonymous> (/srv/node_modules/graphql-request/dist/src/index.js:108:25)
    at step (/srv/node_modules/graphql-request/dist/src/index.js:44:23)
    at Object.next (/srv/node_modules/graphql-request/dist/src/index.js:25:53)
    at /srv/node_modules/graphql-request/dist/src/index.js:19:71
    at new Promise (<anonymous>)
    at __awaiter (/srv/node_modules/graphql-request/dist/src/index.js:15:12)
    at GraphQLClient.request (/srv/node_modules/graphql-request/dist/src/index.js:98:16)
    at exports.processSignUp.functions.auth.user.onCreate (/srv/index.js:60:25)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:132:23)
    at /worker/worker.js:825:24 

我已经尝试了几乎所有我能想到的东西。我已经使用https://hasura.io/jwt-config/在Heroku上设置JWT。我已经三遍检查了密码和graphQL端点。在hasura控制台中玩耍时,我对突变或查询变量没有任何问题,但无法将Firebase函数连接到hasura。提前致谢。

functions / index.js

...

const client = new request.GraphQLClient(
  "https://app-name.herokuapp.com/v1/graphql",
  {
    headers: {
      "content-type": "application/json",
      "x-hasura-admin-secret": "Password",
    },
  }
);
...
// On register.
exports.registerUser = functions.https.onCall((data) => {
  const { email, password } = data;

  try {
    const userRecord = admin.auth().createUser({ email, password });

    const customClaims = {
      "https://hasura.io/jwt/claims": {
        "x-hasura-default-role": "user",
        "x-hasura-allowed-roles": ["user"],
        "x-hasura-user-id": userRecord.uid,
      },
    };

    admin.auth().setCustomUserClaims(userRecord.uid, customClaims);
    return userRecord.toJSON();
  } catch (e) {
    let errorCode = "unknown";
    let msg = "Something went wrong, please try again later";
    if (e.code === "auth/email-already-exists") {
      errorCode = "already-exists";
      msg = e.message;
    }
    throw new functions.https.HttpsError(errorCode, msg, JSON.stringify(e));
  }
});

...

// On sign up.
exports.processSignUp = functions.auth.user().onCreate(async (user) => {
  const { uid: id, email } = user;
  const mutation = `
    mutation($id: String!, $email: String) {
      insert_users(objects: [{
        id: $id,
        email: $email,
      }]) {
        affected_rows
      }
    }
  `;

  try {
    const data = await client.request(mutation, { id, email });

    return data;
  } catch (e) {
    throw new functions.https.HttpsError("invalid-argument", e.message);
  }
});
firebase hasura
1个回答
1
投票

在您的函数的package.json中,尝试将节点引擎更改为10,并将grapql-request软件包更改为1.8.2。

© www.soinside.com 2019 - 2024. All rights reserved.