【问题标题】:Sync user with Firebase functions to Hasura GraphQL将具有 Firebase 功能的用户同步到 Hasura GraphQL
【发布时间】:2020-09-29 15:09:19
【问题描述】:

我想使用 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。提前致谢。

函数/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】:

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

    【讨论】:

    • 感谢一百万!我几乎总是选择我得到的版本,并完全排除更改版本的可能性。学过的知识!和那个打了好几天...
    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多