首页 > 解决方案 > 使用 express nodejs 获取此错误 auth/operation-not-supported-in-this-environment

问题描述

我在我的项目中使用 Firebase,但在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})
 });
}

快递代码

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
      })
   });   
})

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

谢谢!!!

更新(16-10-18)

使用 Gmail 帐户成功登录后调用仪表板路由。

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 登录的新用户,如果我使用简单的用户名和密码登录,则currentUser返回数据。我哪里错了??

2018 年 17 月 10 日更新

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'仪表板并表示呼叫定义的路由。我昨天提到过。现在请告诉我我在哪里调用仪表板路线?

标签: javascriptnode.jsfirebaseexpressfirebase-authentication

解决方案


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

用谷歌登录是在前端完成的,非常简单,你不需要改变你的html代码,只需要像下面这样的javascript,不需要调用云函数

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

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

对于对云函数的经过身份验证的调用,您需要先获取令牌,然后再进行调用。但是只有在您已经登录后才能执行此操作。

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
     })
  });
})

推荐阅读