草庐IT

javascript - 使用 express nodejs 获取此错误 auth/operation-not-supported-in-this-environment

coder 2024-07-19 原文

我在我的项目中使用 Firebase,但在使用 google 凭据登录时出现此错误 auth/operation-not-supported-in-this-environment

.hbs文件代码

<span class="google-bg session-icon">
  <a href="#!" id="google" onclick=" return loginWithGoogle(this)">
      <i class="ti-google"></i>
   </a>
</span>

脚本代码

function loginWithGoogle(event){
  $.ajax({
    url: "/session/google/login",
       type: "POST"
    })
    .done(function (data) {
    error= JSON.stringify(data);
    console.log(error);
    M.toast({html: error})
 });
}

express 代码

router.post('/session/google/login',function (req, res, next){
   //console.log('get resqust');
   firebase.auth().signInWithRedirect(googleAuthProvider).then(function(result){
       console.log(result);
   }, function(error){
      console.log(error.code);
      res.json({
         data: error.code
      })
   });   
})

当我点击 Google 图标然后调用 loginWithGoogle 函数并获取路由器路径 /session/google/login 之后执行 express 代码并生成错误。我想知道如何解决这个问题,可能出了什么问题?

谢谢!!!

已更新(16-10-18)

Gmail账号登录成功后调用dashboard路由

router.get('/dashboard', function(req, res, next) {
      console.log(firebase.auth().currentUser);
      if(firebase.auth().currentUser != null){
         res.render('dashboard', { 
            title: 'App'
         });
      }else {
      req.session.error = 'Access denied!';
      console.log(req.session.error);
      res.redirect('/login');
   }
}); 

使用 gmail 帐户成功登录后,我在呈现仪表板页面之前调用了仪表板路由和使用条件,但 currentUser 返回 null。我检查了 Firebase 控制台,该控制台显示最近使用 gmail 登录的新用户,如果我使用简单的用户 ID 和密码登录,则 currentUser 返回数据。我哪里错了??

17-10-18 更新

function loginWithGoogle(event) {
    firebase.initializeApp(config);
    var provider = new firebase.auth.GoogleAuthProvider();
    firebase.auth().signInWithPopup(googleAuthProvider)
       .then(function (user) {
           // localStorage.setItem('user',JSON.stringify(user));
           window.location.href = '/dashboard'; 
        })
        .catch(function (error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            var email = error.email;
            var credential = error.credential
        })
    }

成功登录后,我已重定向到 '/dashboard' 并为仪表板快速调用定义的路由。我昨天提到过。现在请告诉我在哪里调用仪表板路由??

最佳答案

在你开始之前检查你是否已经将 firebase 添加到你的前端,如果你还没有这样做的话这里有如何做的说明:https://firebase.google.com/docs/web/setup

Google 登录是在前端完成的,非常简单,您不需要更改 html 代码,只需如下所示的 javascript,无需调用云函数

function loginWithGoogle(event){
  var provider = new firebase.auth.GoogleAuthProvider();
  firebase.auth().signInWithRedirect(provider);
}

这只是您通过重定向登录时的一种用例,有弹出式登录和更复杂的登录处理,您可以在使它生效后尝试:https://firebase.google.com/docs/auth/web/google-signin

对于经过身份验证的云函数调用,您需要先获取 token ,然后再进行调用。但您必须先登录后才能执行此操作。

function authenticatedCallToFunctions(event){
  firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
    $.ajax({
        url: "someurl",
        type: "POST",
        headers: {"firebase-token": idToken}
      })
      .done(function (data) {
        error= JSON.stringify(data);
        console.log(error);
        M.toast({html: error})
      });
  }).catch(function(error) {
    // Handle error
  });
}

并且您可以在像这样的云函数中处理经过身份验证的请求

import * as admin from 'firebase-admin';

router.post('someurl',function (req, res, next){
  var token = req.headers['firebase-token'];

  admin.auth().verifyIdToken(token).then(function(result){
      console.log(result);
  }, function(error){
     console.log(error.code);
     res.json({
        data: error.code
     })
  });
})

关于javascript - 使用 express nodejs 获取此错误 auth/operation-not-supported-in-this-environment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52553009/

有关javascript - 使用 express nodejs 获取此错误 auth/operation-not-supported-in-this-environment的更多相关文章

随机推荐