【问题标题】:Is there a way to fix Error: Getting metadata from plugin failed with error: invalid_grant: Invalid JWT有没有办法修复错误:从插件获取元数据失败并出现错误:invalid_grant: Invalid JWT
【发布时间】:2019-10-25 05:44:14
【问题描述】:

我设置了 firebase 云功能以使用 Express,并且还使用 firebase SDK auth 属性进行注册。我注意到使用 firebase-admin 在 firestore 中创建新用户文档的管理员在部署后没有执行。

使用邮递员,我尝试注册一个新用户并从 firebase 收到此错误:

Error: Getting metadata from plugin failed with error: invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems.

我该如何解决这个问题?

我创建了一个登录路由,它工作得很好并返回一个令牌,因为它只调用 firebase SDK auth 属性进行登录

这是我的项目代码:

初始化 firebase-admin


const admin = require("firebase-admin");

const serviceAccount = require("./would-you-rather-app-c5895-firebase-adminsdk-lndjl-f7793c362b.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: `https://${serviceAccount.project_id}.firebaseio.com`
});

const db = admin.firestore(); 
module.exports = { admin, db };

快速设置


const functions = require('firebase-functions');
const app = require("express")();

// const cors = require("cors");
// app.use(cors());

const { signup, login } = require("./handlers/users");

// users routes
app.post("/signup", signup);
app.post("/login", login);

app.get("/", (request, response) => {
  response.send("Hello from Firebase!");
});

exports.api = functions.https.onRequest(app);

用户注册和登录处理程序


const { admin, db } = require("../util/admin");
const config = require("../util/config");

const client = require("firebase");
client.initializeApp(config);

// Sign user up
exports.signup = (req, res) => {
  const newUser = {
    email: req.body.email,
    password: req.body.password,
    confirmPassword: req.body.confirmPassword,
    firstName: req.body.firstName,
    lastName: req.body.lastName,
    fullname: `${req.body.firstName} ${req.body.lastName}`
  };

  // TODO: validation

  const noImg = "no-img.png";
  let userId, token;

  client
    .auth()
    .createUserWithEmailAndPassword(newUser.email, newUser.password)
    .then(data => {
      userId = data.user.uid;
      return data.user.getIdToken();
    })
    .then(idToken => {
      token = idToken;
       const userCredentials = {
        fullname: newUser.fullname,
        createdAt: new Date().toISOString(),
        imageUrl: `https://firebasestorage.googleapis.com/v0/b/${
      config.storageBucket
    }/o/${noImg}?alt=media`,
        score: 0,
        questions: [],
        votes: [],
        userId: userId
      };
      return admin
        .firestore()
        .doc(`users/${userId}`)
        .set(userCredentials);
     })
    .then(() => {
      return res.status(201).json({ token });
    })
    .catch(err => {
      console.log("Signup error: ", err);
      if (err.code === "auth/email-already-in-use") {
        return res.status(400).json({ email: "Email already in use" });
      } else {
        return res
          .status(500)
          .json({ general: "Something went wrong, please try agin" });
      }
    });
};

// Log user in
exports.login = (req, res) => {
  const user = {
    email: req.body.email,
    password: req.body.password
  };

  client
    .auth()
    .signInWithEmailAndPassword(user.email, user.password)
    .then(data => {
      return data.user.getIdToken();
    })
    .then(token => {
      return res.json({ token });
    })
    .catch(err => {
      return res
        .status(403)
        .json({ general: "Wrong credentials, please try again" });
    });
};

运行注册返回错误并验证成功,但用户凭据未添加到 Firestore...

Firebase 调试日志


[info] i  functions: Beginning execution of "api"
[debug] [2019-06-10T17:40:33.997Z] Disabled runtime features: {"functions_config_helper":true,"network_filtering":true,"timeout":true,"memory_limiting":true,"protect_env":true,"admin_stubs":true}
[debug] [2019-06-10T17:40:35.600Z] firebase-admin has been stubbed.
[info] i  Your code has been provided a "firebase-admin" instance.
[debug] [2019-06-10T17:40:36.599Z] Trigger "api" has been found, beginning invocation!
[debug] [2019-06-10T17:40:36.600Z] 
[debug] [2019-06-10T17:40:36.600Z] Running api in mode HTTPS
[debug] [2019-06-10T17:40:36.612Z] {"socketPath":"\\\\?\\pipe\\C:\\Users\\ismail\\Documents\\Archive\\tutorial-codes\\NanoRD\\proj3\\proj3\\functions\\9796"}
[debug] [2019-06-10T17:40:36.617Z] [functions] Runtime ready! Sending request! {"socketPath":"\\\\?\\pipe\\C:\\Users\\ismail\\Documents\\Archive\\tutorial-codes\\NanoRD\\proj3\\proj3\\functions\\9796"}
[debug] [2019-06-10T17:40:36.660Z] Ephemeral server used!
[debug] [2019-06-10T17:40:36.717Z] Ephemeral server survived.
[info] >  Signup error:  Error: Getting metadata from plugin failed with error: invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems.
[info] >      at Http2CallStream.<anonymous> (C:\Users\ismail\Documents\Archive\tutorial-codes\NanoRD\proj3\proj3\functions\node_modules\@grpc\grpc-js\build\src\client.js:101:45)
[info] >      at Http2CallStream.emit (events.js:201:15)
[info] >      at C:\Users\ismail\Documents\Archive\tutorial-codes\NanoRD\proj3\proj3\functions\node_modules\@grpc\grpc-js\build\src\call-stream.js:71:22
[info] >      at processTicksAndRejections (internal/process/task_queues.js:81:9) {
[info] >    code: '400',
[info] >    details: 'Getting metadata from plugin failed with error: invalid_grant: ' +
[info] >      'Invalid JWT: Token must be a short-lived token (60 minutes) and ' +
[info] >      'in a reasonable timeframe. Check your iat and exp values and use ' +
[info] >      'a clock with skew to account for clock differences between ' +
[info] >      'systems.',
[info] >    metadata: Metadata { options: undefined, internalRepr: Map {} }
[info] >  }
[info] i  functions: Finished "api" in ~2s

我希望注册时有一个返回令牌和存储在 firestore 中的新用户凭据

【问题讨论】:

  • 运行它的机器上的时钟似乎不同步。
  • @HiranyaJayathilaka 我如何保持同步?谢谢

标签: express google-cloud-firestore google-cloud-functions firebase-admin


【解决方案1】:

我遇到了这个问题,但它是由我的本地主机上设置的无效时间引起的。

【讨论】:

    猜你喜欢
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 2016-03-12
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    相关资源
    最近更新 更多