繁体   English   中英

使用express nodejs在此环境中获取此错误auth / operation-not-supported-in-environment

[英]Get this error auth/operation-not-supported-in-this-environment using express nodejs

我在我的项目中使用Firebase,但在使用google凭据登录时, auth/operation-not-supported-in-this-environment获取此错误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
      })
   });   
})

当我点击Google图标然后调用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的新用户登录,如果我使用简单的用户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'并为仪表板显示呼叫定义的路由。 我昨天提到过。 现在请告诉我在哪里我称之为仪表板路线?

适用于JavaScript的Firebase身份验证SDK仅适用于浏览器,而不适用于node.js环境。 如果您需要在后端使用Firebase Auth,则需要使用Firebase Admin SDK ,您可以使用它来管理用户会话

在您开始之前,请检查您是否已将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 /文档/认证/网络/谷歌式登入

对于对云功能进行身份验证的呼叫,您需要先获取令牌然后拨打电话。 但是只有在您已经登录后才能这样做。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM