简体   繁体   English

如何在 reactjs 中发送 firebase getIdToken 和 axios?

[英]How can i send firebase getIdToken with axios in reactjs?

I am using the following code to obtain a user idtoken and i can succesfully log it to the console using console.log:我正在使用以下代码获取用户 idtoken,并且可以使用 console.log 成功地将其记录到控制台:

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        user.getIdToken().then(function(idToken) {  
           console.log("IDTOKEN:",idToken); 

          });
    }
});

I am now trying to send the token as a header in a request using axios like this:我现在尝试使用 axios 在请求中将令牌作为 header 发送,如下所示:

async updatebalance() {
  let id = this.props.id;
  if (id === null || id === undefined) {
    id = "test";
  }
  var res = await axios.post(backUrl + "account/user_load_balance", {
    uid: localStorage.getItem("account-info"),
    id: id,
    idToken
  });
  if (res.data !== null && res.data.status && res.data.balance !== null && res.data.balance !== []) {
    this.props.dispatch(etherBalanceLoaded(res.data.balance[0]));
    this.props.dispatch(tokenBalanceLoaded(res.data.balance[1]));
  }
  this.setState({
    showbuttonFlag: true
  });
  this.reset_values();
}

however i get the following error:但是我收到以下错误:

'idToken' is not defined 'idToken' 未定义

I think i may need to set this as a global variable so it can be used in different function but i don't know how.我想我可能需要将其设置为全局变量,以便它可以在不同的 function 中使用,但我不知道如何。 How can i achieve this?我怎样才能做到这一点?

Solution 1:解决方案1:

async updatebalance() {
        let id = this.props.id;
        if (id === null || id === undefined) {
            id = "test";
        }
const user = firebase.auth().currentUser
if (user) {
  const idToken = await user.getIdToken();
  var res = await axios.post(backUrl + "account/user_load_balance", {
    uid: localStorage.getItem("account-info"),
    id: id,
    idToken
  });

} else {
  console.log("User not logged in")
}
        if (res.data !== null && res.data.status && res.data.balance !== null && res.data.balance !== []) {
            this.props.dispatch(etherBalanceLoaded(res.data.balance[0]));
            this.props.dispatch(tokenBalanceLoaded(res.data.balance[1]));
        }
        this.setState({ showbuttonFlag: true });
        this.reset_values();
    }

    reset_values() {
        this.setState({ ethAmount: "" });
        this.setState({ sellethAmount: "" });
        this.setState({ ethPrice: "" });
        this.setState({ sellethPrice: "" });
        this.setState({ methAmount: "" });
        this.setState({ methPrice: "" });
        this.setState({ msellethAmount: "" });
        this.setState({ msellethPrice: "" });
    }

You can use getIdToken() right before the Axios request and pass it as shown below:您可以在 Axios 请求之前使用getIdToken()并传递它,如下所示:

const user = firebase.auth().currentUser

if (user) {
  // user logged in
  const idToken = await user.getIdToken()

  var res = await axios.post(backUrl + "account/user_load_balance", {
    uid: localStorage.getItem("account-info"),
    id: id,
    idToken
  });

  if (res.data !== null && res.data.status && res.data.balance !== null && res.data.balance !== []) {
    this.props.dispatch(etherBalanceLoaded(res.data.balance[0]));
    this.props.dispatch(tokenBalanceLoaded(res.data.balance[1]));
  }
  this.setState({
    showbuttonFlag: true
  });
  this.reset_values();

} else {
  console.log("User not logged in")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase 可以为特定用户提供多少次“currentUser.getIdToken() 和 verifyIdToken()”请求? - How many times can firebase serve, the "currentUser.getIdToken() and verifyIdToken()" requests, for the particular user? 如何从 Firebase Firestore 获取 ReactJs 的数据? - How Can I get data with ReactJs from Firebase Firestore? 如何在身份验证时获取Idtoken - How to getIdtoken on authentication 如何使用 firebase 和 javascript 将图像发送给其他用户? - How can I send image to other user by using firebase and javascript? 在使用 reactjs 发送 POST 请求之前,我如何等待 firebase 检查用户是否有效? - how can i wait for firebase to check the user is valid before sending a POST request with reactjs? Android Studio - 从 GetIdToken 获取 Firebase 令牌 - Android Studio - Get Firebase token from GetIdToken 如何使用 reactjs 在 firebase v9 云存储中上传多张图片 - How can I upload multiple images in firebase v9 cloud storage using reactjs 如何在不使用 Firebase 控制台的情况下发送 Firebase 云消息通知? - How can I send a Firebase Cloud Messaging notification without use the Firebase Console? Firebase Auth UI Android,getIdToken() 返回的任务永远不会完成 - Firebase Auth UI Android, task returned by getIdToken() never completes 在 firebase 中更新密码时出现“TypeError: user.getIdToken is not a function” - Getting "TypeError: user.getIdToken is not a function" when updating password in firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM