2

我正在使用 gotrue-js 对注册和登录用户对我的 netlify 身份做出反应。我收到一个名为“data”的错误:{“code”:401,“msg”:“Invalid token:signature is invalid”}”。我在本地(在 localhost:8888 上)运行它并使用 netlify cli 运行站点并运行函数。要完全清楚,我在终端中收到 200 statusCode 但在响应中我收到以下错误。

登录.js

  const handleSubmit = () => {
        auth.login(login.email,login.password)
        .then(response => {
            const myAuthHeader = "Bearer " + response.token.access_token; //creates the bearer token
            fetch(`/.netlify/functions/update_netlify_user/admin/users/${response.id}`, {
                method: "PUT",
                body: JSON.stringify({}),
                headers: { Authorization: myAuthHeader },
                credentials: "include"
            })
              .then(response => {
                console.log({ response });
              })
          })
      }

update_netlify_user.js

const fetch = require("node-fetch")

exports.handler = async (event, context) => {
  const { identity, user } = context.clientContext;
  const userID = user.sub;
  console.log(context)
  const userUrl = `${identity.url}/admin/users/${userID}`;
  const adminAuthHeader = "Bearer " + identity.token;

  try {
    return fetch(userUrl, {
      method: "PUT",
      headers: { Authorization: adminAuthHeader },
      body: JSON.stringify({ app_metadata: { roles: ["superstar"] } })
    })
      .then(response => {
        return response.json();
      })
      .then(data => {
        console.log("Updated a user! 204!");
        console.log(JSON.stringify({ data }));
        return { statusCode: 204 };
      })
      
  } catch (e) { return e; }
};
4

0 回答 0